0% found this document useful (0 votes)
30 views5 pages

Haseeb

The document describes the grammar rules for a programming language. It defines the syntax and structure of functions, arguments, statements, expressions and other elements. It also shows the process of removing left recursion from the grammar.

Uploaded by

tabintq
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)
30 views5 pages

Haseeb

The document describes the grammar rules for a programming language. It defines the syntax and structure of functions, arguments, statements, expressions and other elements. It also shows the process of removing left recursion from the grammar.

Uploaded by

tabintq
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/ 5

Haseeb-ul-rehman L1F20BSCS0622 CC-G01

Assignment # 02

Grammar
Function  Type identifier < ArgList > CompoundStmt
ArgList  Arg | ArgList ,Arg
Arg  Type identifier
Declaration  Type IdentList ;
Type  int | float
IdentList  identifier ,IdentList | identifier
Stmt  ForStmt | WhileStmt | Expr ;| IfStmt
| CompoundStmt | Declaration | ;
ForStmt  for < Expr ; OptExpr ; OptExpr > Stmt
OptExpr  Expr | 
LoopStmt  loop < Expr > Stmt
IfStmt  agar < Expr > Stmt MagarPart
MagarPart  magar Stmt | 
CompoundStmt  { StmtList }
StmtList  StmtListStmt | 
Expr  Rvalue
Rvalue  Rvalue Compare Mag | Mag
Compare  == | < | > | <= | >= | != | <>
Mag  Mag + Term | Mag – Term | Term
Term  Term * Factor | Term / Factor | Factor
Factor  < Expr > | identifier | number

Removing Left Recursion:


Function  Type identifier < ArgList > CompoundStmt

ArgList  Arg ArgList’


ArgList’  ,Arg ArgList’ | 

Arg  Type identifier


Declaration  Type IdentList ;
Type  int | float
IdentList  identifier ,IdentList | identifier
Stmt  ForStmt | WhileStmt | Expr ;| IfStmt
| CompoundStmt | Declaration | ;
ForStmt  for < Expr ; OptExpr ; OptExpr > Stmt
OptExpr  Expr | 
LoopStmt  loop < Expr > Stmt
IfStmt  agar < Expr > Stmt MagarPart
MagarPart  magar Stmt | 
CompoundStmt  { StmtList }

StmtList  StmtList’
StmtList’  Stmt StmtList’| 

Expr  Rvalue

Rvalue  Mag Rvalue’


Rvalue’  Compare Mag Rvalue’| 

Compare  == | < | > | <= | >= | != | <>

Mag  Term Mag’


Mag’  + Term Mag’ | – Term Mag’ | 

Term  Factor Term’


Term’  * Factor Term’ | / Factor Term’ | 

Factor  < Expr > | identifier | number

Removing Left Factoring and numbering rues:

1) Function  Type identifier < ArgList > CompoundStmt


2) ArgList  Arg ArgList’
3) ArgList’  ,Arg ArgList’
4) ArgList’  
5) Arg  Type identifier
6) Declaration  Type IdentList ;
7) Type  int
8) Type  float
9) IdentList  identifier IdentList’
10) IdentList’  ,IdentList
11) IdentList’  
12) Stmt  ForStmt
13) Stmt  WhileStmt
14) Stmt  Expr ;
15) Stmt  IfStmt
16) Stmt  CompoundStmt
17) Stmt  Declaration
18) Stmt  ;
19) ForStmt  for < Expr ; OptExpr ; OptExpr > Stmt
20) OptExpr  Expr
21) OptExpr  
22) LoopStmt  loop < Expr > Stmt
23) IfStmt  agar < Expr > Stmt MagarPart
24) MagarPart  magar Stmt
25) MagarPart  
26) CompoundStmt  { StmtList }
27) StmtList  StmtList’
28) StmtList’  Stmt StmtList’
29) StmtList’  
30) Expr  Rvalue
31) Rvalue  Mag Rvalue’
32) Rvalue’  Compare Mag Rvalue’
33) Rvalue’  
34) Compare  ==
35) Compare  < Compare’
36) Compare  > Compare’’
37) Compare  !=
38) Compare’  =
39) Compare’  >
40) Compare’  
41) Compare’’  =
42) Compare’’  
43) Mag  Term Mag’
44) Mag’  + Term Mag’
45) Mag’  – Term Mag’
46) Mag’  
47) Term  Factor Term’
48) Term’  * Factor Term’
49) Term’  / Factor Term’
50) Term’  
51) Factor  < Expr >
52) Factor  identifier
53) Factor  number
First & Follow:
First Follow

Function Int,float $

ArgList Int,float >

ArgList’ , >

Arg Int,float , >

Declaration Int,float Magar, for,WhileStmt, <,


identifier, number, agar, {,
Int,float , ; , }

Type Int,float Identifier

IdentList identifier ;

IdentList’ , ;

Stmt for, <, identifier, number, Magar, for, Int, number,


agar, ,WhileStmt {, Int,float,; WhileStmt, <, identifier, agar,
{, },float , ;

ForStmt for Magar, for, Int, number,


WhileStmt, <, identifier, agar,
{, },float , ;

OptExpr <, identifier, number, ;,>

LoopStmt loop

IfStmt agar Magar, , for, Int, number,


WhileStmt, <, identifier, agar,
{, },float , ;

MagarPart Magar, Magar, , for, Int, number,


WhileStmt, <, identifier, agar,
{, },float , ;

CompoundStmt { $, Magar, , for, Int, number,


WhileStmt, <, identifier, agar,
{, },float , ;

StmtList for, Int, number, WhileStmt, }


<, identifier, agar, {,float , ; , 

StmtList’ for, Int, number, WhileStmt, }


<, identifier, agar, {,float , ; ,

Expr <, identifier, number ;,>

Rvalue <, identifier, number ;,>

Rvalue’ =,<,>,!, ;,>

Compare =,<,>,! <, identifier, number

Compare’ =,>, <, identifier, number

Compare’’ =, <, identifier, number

Mag <, identifier, number =,<,>,!,;

Mag’ +,-, =,<,>,!,;

Term <, identifier, number +,-,=,<,>,!,;

Term’ *,/, +,-,=,<,>,!,;

Factor <, identifier, number *,/,+,-,=,<,>,!,;

You might also like