ACD unit-2 part-2
ACD unit-2 part-2
1. Introduction
Syntax Analyzer Vs Lexical
Analyzer
Contents:
3. Lexical errors
For this purpose we use regular expression, a notation that can be used to
describe essentially all the tokens of programminglanguage.
After decided what the tokens are, we need some mechanism to recognize
these in the input stream.This is done by the token recognizers, which are
designed using transition diagrams and finite automata.
Lexical Role of Lexical analyzer :
analysis
The Lexical Analyzer is
the first phase of
acompiler.
Lexical Analyzer may also perform certain secondary tasks as the user interface.
One such task is striping out from the source program the commands and white
spaces in the form of blank, tab and new line characters.
Another is correlating error message from the compiler with the source program.
Lexical analysis
TOKEN,LEXEME, PATTERN:
letter = a/b/c/d/----/z
digit =0/1/2/---/9
delimiter= . /, / ;
Lexical analysis
letter = a/b/c/d/----/z
digit =0/1/2/---/9
delimiter= . /, / ;
Lexical analysis
“ beginning with a digit followed by any number of digits, dot followed by one
or more digits ”
letter = a/b/c/d/----/z
digit =0/1/2/---/9
delimiter= . /, / ;
Lexical analysis
In a particular language key words are defined as :
C=getchar( ); C=getchar( );
If c==letter( c) then goto state 1 If c==letter( c) || digit (c ) then goto
Else state 1
Error( ); Else
If c==delimiter( C ) then goto state
Algorithm code for state 2: Else
2
Error( );
Retract( )
Install ( id )
Lexical analysis
In a similar way algorithmic code was written for all the states in all the
transition diagrams. Later these algorithms are converted to programs.
Now in our program, if we use any key word, it will be given as input to the
particular automata, if the keyword is correctly spelled and allowed as per
language constructs it will enter to final state and it was accepted other wise
error handler was invoked.
Lexical analysis
LEXICALERRORS:
Lexical errors are the errors thrown by your lexer when unable to continue.
Which means that there's no way to recognize a lexeme as a valid token for
you lexer.
Syntax errors, on the other side, will be thrown by your scanner when a
given set of already recognized valid tokens don't match any of the right
sides of your grammarrules.
Both of them do similar things; But the lexical analyzer deals with
simple non-recursive constructs of the language.
The syntax analyzer deals with recursive constructs of the
language.
The lexical analyzer simplifies the job of the syntax analyzer.
The lexical analyzer recognizes the smallest meaningful units (tokens) in
source program.
The syntax analyzer works on the smallest meaningful units (tokens) in a
source program to recognize meaningful structures in the given
program.
Lexical analyzer tool --LEX
LEX :