Yacc for C Grammar

advertisement
Assignment 3
We define here a programming language called Mini C, which is a suitable
language for a compiler projects. The project in class is divided into three steps:
Scanner, Parser, and Code Generator. Of course source language for the compiler
project is a subset of C, and the output target code is the MIPS codes. In the second step,
we list the BNF context free grammars of the language. You need to use YACC to
construct a parser for Mini C, and your parser must cooperate with scanner in
Assignment2.
Pic1. The CFG(Control Flow Graph) of three assignments
A BNF grammar for Mini C is as follows:
1.
2.
3.
4.
5.
6.
7.
8.
program → declaration-list
declaration-list → declaration-list declaration | declaration
declaration → var-declaration | fun-declaration
var-declaration → type-specifier ID ;
type-specifier → int | void | float
fun-declaration → type-specifier ID ( params ) compound-stmt
params → param-list | void | λ
param-list → param-list , param | param
param → type-specifier ID
compound-stmt → { local-declarations statement-list }
local-declarations →local-declarations var-declaration | λ
statement-list → statement-list statement | λ
statement → expression-stmt | compound-stmt | selection-stmt | iteration-stmt |
jump-stmt
14. expression-stmt → expression ; | ;
15. selection-stmt → if ( expression ) statement
9.
10.
11.
12.
13.
| if ( expression ) statement else statement
iteration-stmt → while-statement | for-statement
while-statement → while ( expression ) statement
for-statement → for ( expression ; expression ; expression ) statement
jump-stmt → return ; | return expression ; | break ;
expression → id-assign = expression | simple-expression
id-assign → ID
simple-expression
→ additive-expression relop additive-expression | additive-expression
23. relop → <= | < | > | >= | == | != | && | ||
24. additive-expression → additive-expression addop term | term
16.
17.
18.
19.
20.
21.
22.
25. addop → + | 26. term → term mulop factor | factor
27. mulop → * | /
28.
29.
30.
31.
32.
33.
34.
factor → ( expression ) | id-assign | call | num
call → ID ( args )
args → arg-list | λ
arg-list → arg-list , expression | expression
num → pos-num | neg-num
pos-num → + value | value
neg-num → - value
35. value → INT_NUM | FLOAT_NUM
Review the token in scanner:
The Keywords of the language are the following:
void
int
float if
else while for
Special symbols are the following:
&&
|| <=
>=
== <
- * / /* */
>
!=
=
return
(
break
)
{
}
;
,
.
Other tokens are INT_NUM, FLOAT_NUM, ID, defined by the following regular
expressions:
digit
=
[0-9]
letter
=
[a-z A-Z]
INT_NUM
=
[+|-]? [digit]+
FLOAT_NUM
=
[+|-]? [digit]+ . [digit]+
ID
=
[letter]+ [digit | letter | _]*
+
1. In Mini parser, if you can get Token-List from Assignment 2 (Scanner generate
from LEX), you can get 45% score
2. If you can show the correctness of program, you can get 60% score.
3. If you can not only show correctness of program, but construct a parsing tree. You
can get 75% score.
4. Comment in Parser should be avoided.
Format
1. Technique Report
Please turn in a report includes
- Cover
- Purpose
- Method
- Results
- What you have learned
You need to explain how to execute your program in the report. (less than 10 pages)
2. Source Code
Please upload your work to course FTP before due date!
Course FTP:
IP: 140.114.71.192
Port: 21
Account: compiler
Password: 340400
Create a package named as your student ID
Your package should include:
/Source code
/Makefile // If you need .
/Readme
Evaluation
Technique Report
20% (A4 not more than 10 pages)
Source Code
80%
(Base : 60% + Output Display & Source Code Comments : 20% )
Source Code
Due date: 2012 / 5 / 10 12:00:00
Report
Due date: 2012 / 5 / 10 In class.
Late submission policy: 1 Day late:-10% 2 Day late:-20% 3 Day late:-30%
Assignment will not be accepted after three days.
Does not copy or you will get the point: (points / the # of people of the same copy)
We only provide Windows environment. You should bring your laptop for different
environment.
If you have any questions about HW, please send mail to TA.
Download