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.  program → declaration-list

2.  declaration-list → declaration-list declaration | declaration

3.  declaration → var-declaration | fun-declaration

4.  var-declaration → type-specifier ID ;

5.  type-specifier → int | void | float

6.  fun-declaration → type-specifier ID ( params ) compound-stmt

7.  params → param-list | void | λ

8.  param-list → param-list , param | param

9.  param → type-specifier ID

10.  compound-stmt → { local-declarations statement-list }

11.  local-declarations →local-declarations var-declaration | λ

12.  statement-list → statement-list statement | λ

13.  statement → expression-stmt | compound-stmt | selection-stmt | iteration-stmt | jump-stmt

14.  expression-stmt → expression ; | ;

15.  selection-stmt → if ( expression ) statement
| if ( expression ) statement else statement

16.  iteration-stmt → while-statement | for-statement

17.  while-statement → while ( expression ) statement

18.  for-statement → for ( expression ; expression ; expression ) statement

19.  jump-stmt → return ; | return expression ; | break ;

20.  expression → id-assign = expression | simple-expression

21.  id-assign → ID

22.  simple-expression
→ additive-expression relop additive-expression | additive-expression

23.  relop → <= | < | > | >= | == | != | | ||

24.  additive-expression → additive-expression addop term | term

25.  addop → + | -

26.  term → term mulop factor | factor

27.  mulop → * | /

28.  factor → ( expression ) | id-assign | call | num

29.  call → ID ( args )

30.  args → arg-list | λ

31.  arg-list → arg-list , expression | expression

32.  num → pos-num | neg-num

33.  pos-num → + value | value

34.  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 return break

Special symbols are the following:

|| <= >= == < > != = ( ) { } ; , . + - * / /* */

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 reportincludes

- 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.