Dec 232017
Example PROLOG code that will parse code and analyze it. The language of the code it parses is a theoretical one which is defined in the docs. | |||
---|---|---|---|
File Name | File Size | Zip Size | Zip Type |
PARSER.DAT | 718 | 245 | deflated |
PARSER.PRO | 2749 | 779 | deflated |
PARSER.TXT | 2433 | 871 | deflated |
Download File PARSER2.ZIP Here
Contents of the PARSER.TXT file
Using Prolog, build a translator for the language specified by the
following grammar.
Syntax Translation
S ::= program Id; Block m(Block)
Block ::= begin StmtList end m(StmtList);[]
StmtList ::= Stmt ; StmtList m(Stmt);m(Stmtlist)
| Stmt ; m(Stmt)
Stmt ::= Id := Exp assign(Id,m(Exp))
Stmt ::= while Exp do Block while(m(Exp),m(Block))
Stmt ::= IfHead ElsePart if(m(IfHead),m(ElsePart))
IfHead ::= if Exp then Block (m(Exp),m(Block))
ElsePart ::= else Block m(Block)
| []
Exp ::= Term Op Exp expr(m(Op),m(Term),m(Exp))
| Term m(Term)
Term ::= ( Exp ) m(Exp)
| Id Id
| Integer Integer
Op ::= + +
| * *
| - -
| / /
| = =
| > >
| < <
| <= <=
| >= >=
| != !=
For example, the translation of
program a;
begin
b := 1;
c := b + 3;
end
is assign(b,1);assign(c,expr(+,b,3));[].
Your program must have a go(In) clause that reads your program's
input from a file, echos the input, parses the input, and prints
the translation. The input should be a Prolog clause containing
a list of tokens representing the program to be translated, e.g.,
pgm([program,factorial,;,
begin,
value, :=, 10, ;,
count, :=, 1, ;,
while, count, <, value, do, begin,
count, :=, '(', count, +, 1, ')', ;,
result, :=, result, *, count, ;,
end, ;,
end]).
The output produced for this program is shown below:
| ?- go(x).
{consulting ...}
Parsing:
[program,factorial,;,begin,value,:=,10,;,count,:=,1,;,while,count,<,value,do,
begin,count,:=,(,count,+,1,),;,result,:=,result,*,count,;,end,;,end]
Results:
assign(value,10);assign(count,1);while(expr(<,count,value),(assign(count,
expr(+,count,1));assign(result,expr(*,result,count));[]));[]
following grammar.
Syntax Translation
S ::= program Id; Block m(Block)
Block ::= begin StmtList end m(StmtList);[]
StmtList ::= Stmt ; StmtList m(Stmt);m(Stmtlist)
| Stmt ; m(Stmt)
Stmt ::= Id := Exp assign(Id,m(Exp))
Stmt ::= while Exp do Block while(m(Exp),m(Block))
Stmt ::= IfHead ElsePart if(m(IfHead),m(ElsePart))
IfHead ::= if Exp then Block (m(Exp),m(Block))
ElsePart ::= else Block m(Block)
| []
Exp ::= Term Op Exp expr(m(Op),m(Term),m(Exp))
| Term m(Term)
Term ::= ( Exp ) m(Exp)
| Id Id
| Integer Integer
Op ::= + +
| * *
| - -
| / /
| = =
| > >
| < <
| <= <=
| >= >=
| != !=
For example, the translation of
program a;
begin
b := 1;
c := b + 3;
end
is assign(b,1);assign(c,expr(+,b,3));[].
Your program must have a go(In) clause that reads your program's
input from a file, echos the input, parses the input, and prints
the translation. The input should be a Prolog clause containing
a list of tokens representing the program to be translated, e.g.,
pgm([program,factorial,;,
begin,
value, :=, 10, ;,
count, :=, 1, ;,
while, count, <, value, do, begin,
count, :=, '(', count, +, 1, ')', ;,
result, :=, result, *, count, ;,
end, ;,
end]).
The output produced for this program is shown below:
| ?- go(x).
{consulting ...}
Parsing:
[program,factorial,;,begin,value,:=,10,;,count,:=,1,;,while,count,<,value,do,
begin,count,:=,(,count,+,1,),;,result,:=,result,*,count,;,end,;,end]
Results:
assign(value,10);assign(count,1);while(expr(<,count,value),(assign(count,
expr(+,count,1));assign(result,expr(*,result,count));[]));[]
December 23, 2017
Add comments