Beruflich Dokumente
Kultur Dokumente
SCSX4010
List of Experiments
1. Study of LEX&Yacc tools.
2. Implementation of lexical analyser generator using LEX.
3. Evaluation of given arithmetic expression using LEX&YACC.Hint:Use unambiguous grammar.
4. Implementation of Recursive decent parsing and parse an input string.
5. Implementation of Operator precedence parsing.
6. Design of Macroprocessor.
7. Implement top down parsing table for the grammar
E E + T| T ; T T*F | F ; F (E) | a ;
8 Design of Pass1 assembler.
9. Design of Pass2 assembler.
10. Implement the back end of the compiler which takes three address statements and produce
assembly language code.
11. Implementation of loader.
EX.NO.1
LEX COMPILER
LEX PGM-----
GCC
LEX.YY.C --
I/P TEXT-----
a.out
-----LEX .YY.C
---A.OUT
----- O/P TEXT
Definitions All code between %{ and %} is copied to the beginning of the resulting C file.
Rules A number of combinations of pattern and action: if the action is more than a single
command it needs to be in braces.
Code This can be very elaborate, but the main ingredient is the call to yylex, the lexical
analyser. If the code segment is left out, a default main is used which only calls
yylex.
3 Definitions section
There are three things that can go in the definitions section:
C code Any indented code between %{ and %} is copied to the C file. This is typically
used for defining file variables, and for prototypes of routines that are defined in the
code segment.
Definitions A definition is very much like a #define cpp directive. For example
letter [a-zA-Z]
digit [0-9]
punct [,.:;!?]
nonblank [ \t]
These definitions can be used in the rules section: one could start a rule
{letter}+ {...
State definitions If a rule depends on context, its possible to introduce states and incorporate
those in the rules. A state definition looks like %s STATE, and by default a
state INITIAL is already given
4. Rules section
The rules section has a number of pattern-action pairs.
4.1 Matched text
When a rule matches part of the input, the matched text is available to the programmer as
a variable char* yytext of length int yyleng.
%{
int charcount=0,linecount=0,wordcount=0;
%}
letter [ \t\n]
%%
{letter}+ {wordcount++; charcount+=yyleng;}
. charcount++;
\n {linecount++; charcount++;}
5 Regular expressions
/* Regular expressions */
White
Letter
[\t\n ]+
[A-Za-z]
digit10
[0-9] /* base 10 */
digit16
[0-9A-Fa-f] /* base 16 */
identifier
{letter}(_|{letter}|{digit10})*
int10
{digit10}+
The example by itself is, I hope, easy to understand, but lets have a deeper look into regular expressions.
Symbol
|
------------------------x
.
[xyz]
Meaning
+------------------------------------|
The "x" character
|
Any character except \n
|
Either x, either y, either z
[^bz]
[a-z]
[^a-z]
R*
R+
R?
R{2,5}
R{2,}
R{2}
"[xyz\"foo"
{NOTION}
\X
|
|
|
|
|
|
|
|
|
|
|
|
|
\0
\123
\x2A
RS
R|S
R/S
^R
R$
<<EOF>>
|
|
|
|
|
|
|
|
|
Conclusion:
With the help of lexical analyser generator tool ,we can separate tokens auttomatically.
EXERCISE 2 :IMPLEMENTATION OF LEXICAL ANALYZER USING LEX
AIM:To perform token separation by writing patterns and actions using LEX.
ALGORITHM:
1. Declare the necessary symbols and header files needed.
2. Write the pattern and corresponding actions to be taken
2.1.
2.2.
2.3.
2.4.
3. In the main function declare a file pointer to read input file and call yylex() to find matching patterns and
corresponding actions to be taken.
Compilation and execution Procedure:
$ lex <lex file name>
$ gcc lex.yy.c ll
$ ./ a.out
ALGORITHM:
1. Declare the necessary header files
2. Write the corresponding patterns and actions and return to yacc
3. In yacc, declare the tokens and associativity of operators globally
4. In the declaration part write the production rules(using ambiguous and unambiguous
grammar) as patterns and prompt accept if the input matches the pattern in the action
part.
5. Call yyparse() in the main function to do parsing.
6.
Compilation and execution Procedure:
$ lex <lex file name>
$ yacc <yacc file name)
$ gcc lex.yy.c y.tab.c ll
$ ./a.out
EXERCISE 4 :Implementation of Recursive Decent Parsing
AIM:
ALGORITHM:
Step 1:Read the expression
Step 2:Write a recurive procedure for each and every non terminal for the given grammar.
Step 3:Print the intermediate steps for the given expression if it is vailid.
Step 4:else print error.
EXERCISE 5: OPERATOR PRECEDENCE PARSER
AIM: To write a program to implement operator precedence for the given expression.
ALGORITHM:
Step 1:Get the expression.
Step 2:Build operator precedence relation table
Step 3:set ip to point to the first symbol of w$
Repeat forever
If $ is on top of the stack and ip points to $ then
return;
else begin
let a be the topmost terminal symbol on the stack and let b the symbol pointed to
by ip
if a< b or a=b then begin
push b to the stack;advance the ip pointer;end;
else if a>b then
repeat
pop the stack
until the top stack terminal is related by < to the terminal most recently popped.
else
error();
end;
step 4: print operator predence relation table and parsing table for a valid expression;
step 5: print error if the expression is invalid.
step 6:stop.
EXERCISE 6 :MACRO PREPROCESSOR
AIM:Write a program to implement the macro preprocessor.
ALGORITHM:
Step 1: Identify macro calls in the program.
Step 2:Determine the values of formal parameters.
Step 3:Maintain the values of expansion time variables declared in a macro.
Step 4: Organize expansion time control flow.
Step 5: Determine the values of sequencing symbols.
Step 6: Perform expansion of a model statement.
EXERCISE 7: Implementation of top down parsing table
Step 1:Read the expression to be parsed for an unambiguous grammar.
Step 2:Consrtuct FIRST & Follow functions for a set of grammar symbols for the grammar.
mnemonic-code
operand1, operand2
Pass 1:
Step 1: Let LC=0 and open source file and create the mnemonic table(MT).
Step 2: Read a statement from input file.
Step 3: Check the given statement s label field for a label and if there is a label store
label name and LC value in symbol table(ST).
Step 4: Search for the given statements mnemonics field in the MT and if a match is
found as DB/DW/EQU assign operand field value in the ST as specified
in the pseudo routine field. If END pseudo-op is encountered go to
step 7.
Step 5: Search the given statements mnemonics field in the MT. If a match is found