Compiler Lab 2

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Lab Assignment – 2

Antlr
Antlr is a syntax analyzer/parser generator which takes a
language definition in the form of LL(*) grammar and generates
the corresponding parser in Java. The Java program when
executed, takes the input program and checks if it conforms to
the grammar.

Antlr has an online interface at http://lab.antlr.org/ which looks


as shown below.

 Lexer and Parser rules can be stated in the left.


 The input program can be provided at the top-right.
 When Run button is clicked, the Java program
corresponding to the grammar is generated and it is
compiled and executed with the Input program.
 If the grammar is right, the parse tree is generated below
or else it shows an error.
The web user interface of Antlr Lab is shown below.
To begin with try the following grammar as (Example 1) stated
in the left (lexical rules) and right (syntactic rules).

Work out the following questions.

1. Try with wrong inputs and see that the parse tree
does not get generated. Observe the errors thrown.
2. Enhance the grammar and add rules for accepting an
integer also along with identifiers.

Example 2:

Exercises

1. Come out with a complete set of rules in LL(1)


grammar for an assignment with all sorts of possible
expressions.
a. Directly coming out with LL(1) grammar can be very
difficult. First come out with rules in CFG and then
turn it into LL(1).
 Right now, you have to apply your mind to
do this conversion.
 After we cover FIRST and FOLLOW, you may be
able to do it easier.
b. Improve the rules bit-by-bit to cover and generalize
one step at a time. Add appropriate lexical rules as
necessary.
c. Here are the series of increasingly general
assignment statement syntax.
d. Ensure that parse tree is correct, and you can make
sense out of it with respect to the grammar and input
you provided.
 Simple assignment: a = b
 RHS has expression with one operation: a = b + c
 RHS can contain expression with either a
variable or a number:
 a = b + c
 a = b + 10
 a = 20 + c
 a = 10 + 20
 Add rules for accommodating ‘real’ numbers:
 a = b + 10.5
 a = 20.75 + c
 a = 10.333 + 20.667
 a = 20.75 + 10
 Add support of minus (-) operator:
 a = b - 10.55
 a = 20.75 - c
 a = 10.333 - 20.667
 Add support for mul (*), div (/) and rem (%) operators:
 a = 25 * 4, a = b / 10
 a = b % c
 RHS has any number of terms:
 a = b + 20 - d * e / 10
 a = b + 20.15 - d * e / 10.5
 a = 22 / 7 * r * r
 Add support for precedence: + and - done last,
* and / done before:
 a = b + c * d
 a = b / c + d
 Add support for associativity:
 a = b - c - d (Note: First b-c to be done
- left associative)
 a = b / c % d (Note: c % d should be done
first. % is right associative)
 …….
2. Come out with complete set of rules for declaration statement.
. Simple declarations
 int a
 float b
 char ch
 string str
 bool b
a. Multi variable declarations
 int a, b, c, d
 similarly float, char, string, bool
b. Declaration + Definition
 float a = 2.5, b, c = 3.78
 bool b = True, c = False
c. Array declaration
 char a[10]
 float b[n]
 string s[ ] = { ‘amrita’, ‘vishwa’, ‘vidyapeetham’ }
d. Pointer declaration
 int * p
 int * p = q
 char **r
3. Add rules for recognizing boolean expression (bool-expr).
o a == b
o a != b
o a < b
o a <= b
o a > b
o a >= b
4. Add rules for recognizing expressions with boolean operators.
o a == b && c < d
o a != b || c > d
o !a == b || c == !d
5. Come out with complete set of rules for if-else construct.
. Simple branching: if ( a > = b ) { a = b }
a. Two-way branching: if ( a != b ) { a = b } else { a = a + b }
b. Multi-way branching: if ( a < b ) { a = b } else if ( a
<= b ) { a = b - a } else { a = b }
c. Nested branching: if ( a == b ) { if ( c > d ) { a = b + c }
else { a
= b + d } } else { if ( e > f ) { a = b + e } else { a = b +
f}}
6. Come out with complete set of rules for while loop.
(Elaborate and try)
7. Come out with complete set of rules for for loop. (Elaborate and
try)
8. Come out with complete set of rules for function
definition. (Elaborate and try)
9. Come out with complete set of rules for function call.
(Elaborate and try)
10. Come out with complete set of rules for class
definition. (Elaborate and try)

You might also like