Sie sind auf Seite 1von 9

SATHYABAMA UNIVERSITY

SCSX4010

SYSTEM PROGRAMMING LAB

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

STUDY OF LEX TOOL

AIM: To study about lexical analyser generator [LEX TOOL].


1.Introduction: The unix utility lex parses a file of characters. It uses regular expression matching; typically
it is used to tokenize the contents of the file. In that context, it is often used together with the yacc utility.
2 Structure of a lex file
A lex file looks like
...definitions...
%%
...rules...
%%
...code...
Here is a simple example:
%{
int charcount=0,linecount=0;
%}
%%
. charcount++;
\n {linecount++; charcount++;}
%%
int main()
{
yylex();
printf("There were %d characters in %d lines\n",
charcount,linecount);
return 0;
}
2.1 Block Diagram:

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>>

|
|
|
|
|
|
|
|
|

Any character, EXCEPT b and z


Any character between a and z
Any character EXCEPT those between a and z
Zero R or more; R can be any regular expression
One R or more
One or zero R (that is an optionnal R)
Two to 5 R
Two R or more
Exactly two R
The string "[xyz"foo"
Expansion of NOTION, that as been defined above in the file
If X is a "a", "b", "f", "n", "r", "t", or
"v", this represent the ANSI-C interpretation of \X
ASCII 0 character
ASCII character which ASCII code is 123 IN OCTAL
ASCII character which ASCII code is 2A in hexadecimal
R followed by S
R or S
R, only if followed by S
R, only at the beginning of a line
R, only at the end of a line
End of file

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.

if the pattern is #,* print it as a header file


if the pattern is int|float|char|return|main| print it as a keyword
if the pattern is [][a-zA-z][] print it as a string
Similarly identify patterns and actions for all tokens.

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

EXERCISE 3 :EVALUATION OF ARITHMETIC EXPRESSION USING YACC AND LEX


AIM:

To perform parsing using YACC and LEX.

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:

To perform parsing using Recursive decent parser

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.

Step 3:Construct predictive parsing table using following steps.


3.1:For each production A-> of the grammar do the following steps
3.2:For each terminal a in First(),add A-> to M[A,a]//Where M[A,a] is a parsing table entry.
3.3:if in First(),add A-> to M[A,b] for each terminal b in Follow[A].if is in First() and
$ in Follow(A),add A-> to M[A,$].
3.4. make each undefined entry of M be error.
Step 4:With the help of parsing table( step 3) parse the input string.
Step 5:print parsing table for a valid expression;
step 6: print error if the expression is invalid.
step 7:stop.
EXERCISE 8 &9: DESIGN OF PASS I and pass II ASSEMBLER
AIM: To design a pass I and Pass II assembler
ALGORITHM:
Assume that assembler supports pseudo instructions like ORG, START, END, EQU,DC & DS
instructions only.
Input file format is given as
Label-field

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

find out the length(L) of the instruction


Step 6: Update LC value as LC=LC+L and repeat steps 2-6 till the end of file is
encountered.
Step 7: Reset the source file.
Pass 2:
Step 1: Initialize LC to 0.Open the source file.
Step 2: Read the instruction mnemonic field.
Step 3: Search for the mnemonic field in MT. If a match is found then perform the
appropriate action specified else if END is found goto step 6 .
Step 4: Search for the mnemonic field in the MT. If match is found then get the
hexcode and the length of the instruction. Write the hexcode and the LC value in
the object file. Evaluate all operands and literals and place it in the object file.
Step 5: Update LC vale as LC=LC+L and repeat steps 2-6 till the end of the file.
Step 6: Reset the source file.
EXERCISE 10:Intermediate Code Generation
Input:three address statement in the form x:=y op z
Output:assembly code
Step 1:Read the statement
Step 2:Invoke a function getreg to detemine the location L where the result of the compuation y op z should
be stored.
Step 3:Consul the address descriptor for y to determine y.If the value of y is not in L,generate the instruction
Mov y,L to place a copy of y in L.
Step 4:Generate the instruction Op z,L.Update address and register descriptor.
Step 5:If the current value of y and z have no next uses, then after execution x:=y op z,those registers no
longer will contain y and z.
Step 6:stop.

EXERCISE 11:Implementation of loader


Step 1:start
Step 2:Store the assembly code in a file along with its address.
Step 3:Read the relocatable address
Step 4:Find relocation factor.
Step 5:open a file.
Step 6:Read each and every line and add its address with relocation factor.
Step 7:Repeat above steps until END is encountered.
Step 8:stop.

Das könnte Ihnen auch gefallen