0% found this document useful (0 votes)
8 views3 pages

Spring 2025 - CS606 - 1

The document outlines an assignment for a compiler construction course, focusing on designing a context-free grammar (CFG) for a simplified scripting language that includes variable declarations, arithmetic operations, and if statements. It provides a list of non-terminals and terminals, along with CFG rules for constructing valid programs. Additionally, it includes a derivation of a code snippet using the defined CFG in a tabular format.

Uploaded by

junaidbhatti1218
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views3 pages

Spring 2025 - CS606 - 1

The document outlines an assignment for a compiler construction course, focusing on designing a context-free grammar (CFG) for a simplified scripting language that includes variable declarations, arithmetic operations, and if statements. It provides a list of non-terminals and terminals, along with CFG rules for constructing valid programs. Additionally, it includes a derivation of a code snippet using the defined CFG in a tabular format.

Uploaded by

junaidbhatti1218
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

CS606 – Compiler Construction

Total Marks: 20
Assignment No. 01
Due Date: May 12, 2025
Semester: Spring 2025

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.

Answer:

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.

1
CS606 – Compiler Construction
Total Marks: 20
Assignment No. 01
Due Date: May 12, 2025
Semester: Spring 2025

CFG RULES:
1. Program → StmtList
2. StmtList → Stmt StmtList | ε
3. Stmt → DeclStmt | AssignStmt | IfStmt
4. DeclStmt → Type ID = Expr ;
5. AssignStmt → ID = Expr ;
6. IfStmt → if ( Expr ) {StmtList}
7. Expr → Term RelOp Term
8. Term → Term + Factor
9. Term → Factor
10. Factor → id
11. RelOp → > | < | == | != | >= | <=
12. Type → int

a)[b)] Provide a derivation of a short code snippet using your CFG in a tabular form, as given below:

Code Snippet:

int x = 5;

if (x > 0)

x = x + 1;

Step Rule Applied Sentential Form

1 Program Program

2 1 StmtList

3 ? ?

2
CS606 – Compiler Construction
Total Marks: 20
Assignment No. 01
Due Date: May 12, 2025
Semester: Spring 2025

Answer:

Ste Rule Applied Sentential Form


p

1 1 Program

2 Program → StmtList StmtList

3 2 Stmt StmtList

4 4 Decl StmtList

5 7 Type ID = Expr ; StmtList

6 - Int x = Expr ; StmtList


6 9, 10 int x = 5 ; StmtList

7 2 int x = 5; Stmt StmtList

8 6 int x = 5; IfStmt StmtList

9 6 int x = 5; if (Cond) Stmt StmtList

10 7 int x = 5; if (Expr RelOp Expr) Stmt StmtList

11 9, 10 int x = 5; if (x > 0) Stmt StmtList

12 5 int x = 5; if (x > 0) AssignStmt StmtList

13 5 int x = 5; if (x > 0) x = Expr ; StmtList

14 9 int x = 5; if (x > 0) x = Expr + Term ; StmtList

15 10, 10 int x = 5; if (x > 0) x = x + 1; StmtList

16 2 (ε) int x = 5; if (x > 0) x = x + 1;

You might also like