Sie sind auf Seite 1von 6

Software Construction

Final Assignment
Left Recursive Predictive Parser
1. By writing context free grammar, create a structured programming language which must have the
following characteristics, you may ignore left recursion and ambiguity:
[50 marks]

Declaration Statement
o Single and multiple declaration
o Array declaration
Expressions
Assignment Statement
Conditional Statement (If-else)
Loops (for, while)
Functions
Code
CodeStatmentsList
CodeStatment
DataType
LexemesList
Lexeme
CodeBlock
CodeBlockRange
CodeRange

DeclerationRangeStam
ent
PointersOption
DeclerationStament
DeclerationStamentList
Decleration
IfRangeStatment
FcalRangeStatment
ReturnRangeStatment
LoopRangeStatment

CodeStatmentsList|
CodeStatment CodeStatmentsList|
CodeStatment
DataType id (LexemesList) CodeBlock
int | char| float | string | bool
Lexeme , LexemesList | Lexeme |
id | literal | num
{ CodeBlockRange }
CodeRange CodeBlockRange | CodeRange
DeclerationRangeStament
| IfRangeStatment
| LoopRangeStatment
| FcalRangeStatment
| ReturnRangeStatment
| BreakRangeStatment
| ExpressionRangeStatment
DataType
PointersOption
DeclerationStament;
*|
DeclerationStamentList | Decleration
Decleration,
DeclerationStamentList
|
Decleration
id | id [ num ]
if Expression CodeBlock
| if Expression CodeBlock else CodeBlock
call id ( LexemesList );
return; | return (Lexeme); | return Lexeme ;
WhileStatment
|
ForStatment
|

Software Construction

WhileStatment
ForStatment

ForOption
DoWhileStatment
ForEachStatment
ForeachIn
ExpressionRangeStatm
ent
BreakRangeStatment
Expression

Operator

DoWhileStatment | ForEachStatment
while Expression CodeBlock
for ( DataType id = num; id < num; id
ForOption )
++ | -do CodeBlock while (Expression)
foreach (DataType id ForeachIn id)
in
Expression ;

break ;
Expression
Operator
Expression
(Expression) | id | literal | num
| id = Expression
+ | - | * | / | > | >= | < | <= | != | =

2. Re-write the grammar by removing left recursion and ambiguity, if exists


Code
CodeStatmentsList
CodeStatment
CodeStatment
Datatype
LexemesList
LexemesList
Lexeme
CodeBlock
CodeBlockRange
CodeRange
CodeRange

DeclerationRangeStam
ent
PointersOption
DeclerationStament
DeclerationStamentList
DeclerationStamentList

Decleration
Decleration

[10 marks]

CodeStatmentsList |
CodeStatment CodeStatment
CodeStatmentList|
DataType id (LexS) CodeBlock
int | char| float | string | bool
LexemesList, LexemesList|
, LexemesList |
id | literal | num
{ CodeBlockRange }
CodeRange CodeRange
CodeBlockRange |
DeclerationRangeStament
| IfRangeStatment
| LoopRangeStatment
| FcalRangeStatment
| ReturnRangeStatment
| BreakRangeStatment
| ExpressionRangeStatment
DataType
PointersOption
DeclerationStament;
*|
DeclerationStamentList | Decleration
Decleration DeclerationStamentList
, DeclerationStamentList |

id Decleration
[num] |

Software Construction

IfRangeStatment

if Expression CodeBlock IfRangeStatment

IfRangeStatment
FcalRangeStatment
ReturnRangeStatment
ReturnRangeStatment
LoopRangeStatment

WhileStatment
ForStatment

ForOption
DoWhileStatment
ForeachStatment
ForeachIn
ExpressionRangeStatm
ent
BreakRangeStatment
Expression
Expression
Expression_
Operator

else CodeBlock |
call id ( LexemesList );
return ReturnRangeStatment;
(Lexeme) | Lexeme |
WhileStatment
|
ForStatment
|
DoWhileStatment | ForeachStatment
while Expression CodeBlock
for ( DataType id = num; id < num; id
ForOption)
++ | -do CodeBlock while (Expression)
foreach (DataType id ForeachIn id)
in
Expression ;

break ;
Expression_ Expression
Operator Expression
(Expression) | id | literal | num
+ | - | * | / | > | >= | < | <= | != | =

3. Evaluate first and follows, and develop a parsing table as a text file.
Symbol
Code
CodeStatmentsList
CodeStatmentsList
CodeStatment
Datatype
LexemeList
LexemeList
Lexeme
CodeBlock
CodeBlockRange

CodeBlockRange

First
int char float string
bool double
int char float string
bool double
int char float string
bool double
int char float string
bool double
int char float string
bool double
id literal num
,
id literal num
{
int char float string
bool double if for
while do foreach
call return break
( id literal num
int char float string

Follow
$
$
$
$
id * $
)$
)$
)$
Else while $
}$

}$

[20 marks]

Software Construction

CodeRange

bool double if for


while do foreach
call return break ( |
id literal num
int char float string
bool double if for
while do foreach
call return break ( |
id literal num
int char float string
bool double

PointersOption
DeclerationStament
DeclerationStamentLis
t
DeclerationStamentLis
t
Decleration
Decleration
IfRangeStatment

*
id
Id

Int char double string


float bool if for while do
foreach call
return
break ( id literal num
}$
Int char double string
float bool if for while do
foreach call
return
break ( id literal num
}$
id $
;$
;$

;$

id
[ num ]
if

IfRangeStatment

else

FcalRangeStatment

call

ReturnRangeStatment

return

ReturnRangeStatment

( id literal num

;$
;$
int char double string
float bool if for while do
foreach call
return
break ( id literal num
}$
int char double string
float bool if for while do
foreach call
return
break ( id literal num
}$
int char double string
float bool if for while do
foreach call return
break ( id literal num
}$
Int char double string
float bool if for while do
foreach call return
break ( id literal num
}$
Int char double string
float bool if for while do

DeclerationRangeSta
ment

Software Construction

LoopRangeStatment

WhileStatment

ForStatment

ForOption
DoWhileStatment
ForeachStatment
ForeachIn
ExpressionRangeStat
ment
BreakRangeStatment
Expression
Expression
Expression _
Operator

4. Code the parsing engine.

foreach call return


break ( id literal num
}$
for while do foreach Int char double string
float bool if for while do
foreach call return
break ( id literal num
}$
while
Int char double string
float bool if for while do
foreach call return
break ( id literal num
}$
for
Int char double string
float bool if for while do
foreach call return
break ( id literal num
}$
++ | -);$
do
);$
Foreach
);$
in
Id $
( id literal num
);$
break
( id literal num

);
+
=
+ - * / > >= < <= +
!= =
=
( id literal num
+
=
+ - * / > >= < <= +
!= =
=

$
=
=
=
=

*
$
*
$
*
$
*
$

/ > >= < <= !


/ > >= < <= !
/ > >= < <= !
/ > >= < <= !

[20 marks]

Software Construction

SUBMISSION
When:
What:
Group:

Just after the final examinations; when the copies will be displayed.
Hardcopies of grammar and its versions, and softcopy of the software.
May consists of 4 to 5 members

Das könnte Ihnen auch gefallen