You are on page 1of 7

DEPEARTMENT OF ELECTRONICS AND COMPUTER SCIENCE ENGINEERING

AUTOMATA AND COMPILER DESIGN


PROJECT BASED LAB REPORT
ON

LR(0) PARSING

BY

V.MADHURIMA - 160050218
Y.YALLI – 160050
V.BHAVANA-
V.PAVANKALYAN-
Y.JASWANTH REDDY-

Section:-3.
INTRODUCTION :
A compiler is a computer program that transforms source code written in a programming
language or computer language (the source language), into another computer language (the
target language, often having a binary form known as object code or machine code).
HOW THE FIRST COMPILER WAS COMPILED :
The first compiler was written (by a human) in an assembly language -- a program called an
assembler would translate assembly language into binary; this is a much simpler process
than compilation because assembly language is just a symbolic form of machine language
that uses opcode.
HISTORY OF COMPILER :
The first theoretical compiler was written by Corrado Bohm, in 1951, for his PhD Thesis. The
first implemented compiler was written by Grace Hopper, who also coined the term
"compiler" referring to her A-0 Sysytem which functioned as a loader or linker, not the
modern notion of a compiler. The FORTAN team led by John W. Backus at IBM introduced
the first commercially available compiler, in 1957, which took 18 person-years to create.
PHASES OF A COMPILER :
PHASE-1: LEXICAL ANALYZER:

The first phase of scanner works as a text scanner. This phase scans the source code as a
stream of characters and converts it into meaningful lexemes. Lexical analyzer represents
these lexemes in the form of tokens as:

<token-name, attribute-value>

PHASE-2: SYNTAX ANALYZER :

The next phase is called the syntax analysis or parsing. It takes the token produced by
lexical analysis as input and generates a parse tree (or syntax tree). In this phase, token
arrangements are checked against the source code grammar, i.e. the parser checks if the
expression made by the tokens is syntactically correct.

PHASE-3: SEMANTIC ANALYZER :

Semantic analysis checks whether the parse tree constructed follows the rules of language.
For example, assignment of values is between compatible data types, and adding string to an
integer. Also, the semantic analyzer keeps track of identifiers, their types and expressions;
whether identifiers are declared before use or not etc. The semantic analyzer produces an
annotated syntax tree as an output.

PHASE-4: INTERMEDIATE CODE GENERATOR :

After semantic analysis the compiler generates an intermediate code of the source code for
the target machine. It represents a program for some abstract machine. It is in between the
high-level language and the machine language. This intermediate code should be generated
in such a way that it makes it easier to be translated into the target machine code.

LR(0) parsing:

Given the following CFG grammar

S → ( L )|a
L → L, S|S

Objectives:

a) To check if this grammar suitable to be parsed using the recursive descendent parsing
method? Justify and modify the grammar if needed.
b) To Compute the FIRST and FOLLOW set of non-terminal symbols of the grammar
resulting from your answer in a)
c) To Construct the corresponding parsing table using the predictive parsing LL(0)
method.
d) To Show the stack contents, the input and the rules used during parsing for the input
string w = (a,a,a)
Sol:

it is left-recursive. You can expand L using a production with L as the left-most symbol
without consuming any of the input terminal symbols. To eliminate this left recursion
we add another non-terminal symbol, L’ and productions as follows:

S → (L)|a
L → S Lʼ
Lʼ → , S Lʼ | ε

FIRST(S) = { ( , a }, FOLLOW(L) = { ) },
FIRST(L) = { ( , a } FOLLOW(S) = { , , ) , $ },

FIRST(L’) = { , , ε} FOLLOW(L’) = { )

VARIABLES FIRST FOLLOW

S {(,a} {)}

L {(,a} {, , ) , $ }

L’ { ,ϵ} {)}

LR(0) Parsing:

S->(L).

(r1)
S’->S.

S’->.S S->(L.)

S->.(L)/.a L->L.,S
L->L,.S

S->.(L)/.a
S->(.L)

L->.L,S/.S

S->.(L)/.a
L->S.

(r4)

S->a L->L,S.

(r2) (r3)
LR Parsing table:

M Action part GoTopart

( ) , a $ S L

0 S2 S3 1

1 ACCEPTENC
E

2 S2 S3 5 4

3 r2 r2 r2 r2 r2
4 S6 S7

5 r4 r4 r4 r4 r4

6 r1 r1 r1 r1 r1

7 S2 S3 8

8 r3 r3 r3 r3 r3

You might also like