



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




PROBLEM STATEMENT:lexical analysis is the process of converting a sequence of charcters from source program into a sequence of charchters from source program into a sequence of tokens. A program which performs lexical analysis is termed as a lexical analyzer,tokenizer or scanner
It takes the modified source code from language preprocessors that are written in the form of sentences. The lexical analyzer breaks these syntaxes into a series of tokens, by removing any whitespace or comments in the source code.
SCANNER: The tokens are passed on to the parser from the lexical analyser.Although scanner is the first phase of compiler,the compilation operation is initiated onliy in the parser .hence the scanner program is not independent routine;it acts as a subroutine for the parser. PARSER: The parser starts checking for the structure or the syntax of the language specified by the context free grammer.whenever we it is need of a token from parsing , it requests the scanner program for the token .In turn the scanner reads the source code ,separates the tokens , and passes in to the parser.the parser then checks for the structure of the sentence specified by the grammer. TOKENS:
Lexemes are said to be a sequence of characters (alphanumeric) in a token. There are some predefined rules for every lexeme to be identified as a valid token. These rules are defined by grammar rules, by means of a pattern. A pattern explains what can be a token, and these patterns are defined by means of regular expressions. In programming language, keywords, constants, identifiers, strings, numbers, operators and punctuations symbols can be considered as tokens. For example, in C language, the variable declaration line int value = 100; contains the tokens: int (keyword), value (identifier), = (operator), 100 (constant) and ; (symbol). Specifications of Tokens
for(j=startid; j<=endid; j++) printf("%c", str[j]); printf(" : Identifier");state=0;i--; } break; KEYWORD CODE: //States for 'if switch(ch) { case1:if(str[i]=='f') state=2; else { state=17; startid=i-1; i--; } case2: if(str[i]=='(' || str[i]==NULL) { printf("if : Keyword"); state=0; i—; } else { state=17; startid=i-2; i--; } break; }
OPERATORS CODE:
if(str[i]=='=') state=24; else { printf("= : Assignment operator"); i--; state=0; } break;
DELIMITERS CODE: /State for special character '(' switch(ch) { case 1: printf("(:Special character"); startid=i; state=0; i--; break; }
/State for special character ')' switch(ch) { case 1: printf(") : Special character"); state=0; i--; break; }
//state for special character ';' switch(ch)