CS606 – Compiler Construction
Assignment No. 01
Semester: Spring 2025
Name:
Vu id:
Suppose that you are developing a simplified compiler for a small scripting
language. The language supports variable declarations, arithmetic operations,
and if statements.
a) Design a CFG that incorporates these features using appropriate
non-terminals and terminals.
b) Provide a derivation of a short code snippet using your CFG in a
tabular form, as given below:
Non-terminals:
Program: The entire program.
StmtList: A list of statements.
Stmt: A single statement (declaration, assignment, or if statement).
Decl: A variable declaration.
Assign: An assignment statement.
IfStmt: An if statement.
Expr: An arithmetic or relational expression.
Term: A term in an arithmetic expression.
Factor: A factor in an arithmetic expression.
RelOp: A relational operator (for conditions in if statements).
Type: The type of a variable (e.g., int).
Terminals:
int: Keyword for integer type.
if: Keyword for if statement.
id: Identifier (variable name, e.g., x).
num: Numeric literal (e.g., 5, 0, 1).
=: Assignment operator.
+: Addition operator.
>: Greater-than operator (for simplicity, we include only > for relational operations).
(: Left parenthesis.
): Right parenthesis.
; Semicolon (statement terminator).
{: Left brace (for if statement body, assuming a block structure).
}: Right brace.
CFG Rules:
1. Program → StmtList
2. StmtList → Stmt StmtList
3. StmtList → ε
4. Stmt → Decl
5. Stmt → Assign
6. Stmt → IfStmt
7. Decl → Type id = Expr ;
8. Assign → id = Expr ;
9. IfStmt → if ( Expr ) { StmtList }
10. Expr → Term RelOp Term
11. Expr → Term
12. Term → Term + Factor
13. Term → Factor
14. Factor → id
15. Factor → num
16. RelOp → >
17. Type → int
Code Snippet:
int x = 5;
if (x > 0)
x = x + 1;
Step Rule Applied Sentential Form
1 Program Program
2 1 StmtList
3 ? ?
Answer
Derivation Table:
Step Rule Applied Sentential Form
1 Program Program
2 1 StmtList
3 2 Stmt StmtList
4 4 Decl StmtList
5 7 Type id = Expr ; StmtList
6 17 int id = Expr ; StmtList
7 - int x = Expr ; StmtList
8 11 int x = Term ; StmtList
9 13 int x = Factor ; StmtList
10 15 int x = num ; StmtList
11 - int x = 5 ; StmtList
12 2 int x = 5 ; Stmt StmtList
13 6 int x = 5 ; IfStmt StmtList
14 9 int x = 5 ; if ( Expr ) { StmtList } StmtList
15 10 int x = 5 ; if ( Term RelOp Term ) { StmtList } StmtList
16 13 int x = 5 ; if ( Factor RelOp Term ) { StmtList } StmtList
17 14 int x = 5 ; if ( id RelOp Term ) { StmtList } StmtList
18 - int x = 5 ; if ( x RelOp Term ) { StmtList } StmtList
19 16 int x = 5 ; if ( x > Term ) { StmtList } StmtList
20 13 int x = 5 ; if ( x > Factor ) { StmtList } StmtList
21 15 int x = 5 ; if ( x > num ) { StmtList } StmtList
22 - int x = 5 ; if ( x > 0 ) { StmtList } StmtList
23 2 int x = 5 ; if ( x > 0 ) { Stmt StmtList } StmtList
24 5 int x = 5 ; if ( x > 0 ) { Assign StmtList } StmtList
25 8 int x = 5 ; if ( x > 0 ) { id = Expr ; StmtList } StmtList
26 - int x = 5 ; if ( x > 0 ) { x = Expr ; StmtList } StmtList
27 12 int x = 5 ; if ( x > 0 ) { x = Expr ; StmtList } StmtList
28 13 int x = 5 ; if( x > 0 ){ x = Factor +Factor ; StmtList}StmtList
29 14,15 int x = 5 ; if ( x > 0 ) { x = id + num ; StmtList } StmtList
30 - int x = 5 ; if ( x > 0 ) { x = x + 1 ; StmtList } StmtList
31 3 int x = 5 ; if ( x > 0 ) { x = x + 1 ; } StmtList
32 3 int x = 5 ; if ( x > 0 ) { x = x + 1 ; }