a simple parser for a simple program language "customer language"
-
A program is a sequence of statements separated by semicolons
-
No procedures and no declarations
-
Data Type: All variables are integer variables, and variables are declared by assigning values to them
-
Statement: Two control statements: if-statement and repeat-statement, read and write statements
-
Expression: Boolean and integer arithmetic expressions
-
Comment: Comments are allowed within curly bracket
Reserved Words | Special Symbols |
---|---|
if then else end repeat until read write ... | + - * / = < ( ) ; := ... |
- program -> declarations stmt-sequence
- declarations -> decl ; declarations |ε
- decl -> type-specifier varlist
- type-specifier-> int | bool | string
- varlist -> identifier { , identifier }
- stmt-sequence -> statement { ; statement }
- statement -> if-stmt | repeat-stmt | assign-stmt | read-stmt | write-stmt | while-stmt
- while-stmt -> do stmt-sequence while bool-exp
- if-stmt -> if exp then stmt-seq end | if exp then stmt-seq else stmt-seq end
- repeat-stmt -> repeat stmt-sequence until exp
- assign-stmt -> identifier:=exp
- read-stmt -> read identifier
- write-stmt -> write exp
- exp -> simp-exp cop simp-exp | simp-exp
- cop -> < | =
- simp-exp -> simp-exp addop term |term
- term -> term mulop factor | factor
- factor -> (exp) |num |id
- addop -> + | -
- mulop -> * | /
{sample program in TINY language- computes factorial}
read x; { input an integer }
if 0<x then { don’t compute if x<=0 }
fact:=1;
repeat
fact := fact*x;
x := x-1;
until x=0;
write fact; {output factorial of x}
end
{this is an example}
int A,B;
bool C;
string D;
D:= 'scanner';
C:=A + B;
do
A:=A*2
while A<=D
{A sample TINY program}
read x;
if 0<x then
fact:=1;
repeat
fact:=fact*x;
x:=x-1
until x=0;
write fact
end
{This is Comment}
read x;{This is Comment}
read y;
if x<y then
x:=10;
repeat
x:=x+1;
y:=y+x
until x=10
end;
repeat
x:=x+1;
if x<y then
y:=y-1
end
until y=x