NCKU Compiler 2021 Assignment 1 Paged
NCKU Compiler 2021 Assignment 1 Paged
NCKU Compiler 2021 Assignment 1 Paged
Your assignment is to write a scanner for the μC language (NOT C language) with lex. This document gives the lexical definition
of the language, while the syntactic definition and code generation will follow in subsequent assignments.
Your programming assignments are based around this division and later assignments will use the parts of the system you have
built in the earlier assignments. That is, in the first assignment you will implement the scanner using lex, in the second
assignment you will implement the syntactic definition in yacc, and in the last assignment you will generate assembly code for
the Java Virtual Machine by augmenting your yacc parser.
This definition is subject to modification as the semester progresses. You should take care in implementation that the
codes you write are well-structured and able to be revised easily.
1. Lexical Definitions
Tokens are divided into two classes:
1.1.1 Delimiters
Delimiters Symbols
Parentheses ( ) { } [ ]
Semicolon ;
Comma ,
Newline \n
Operators Symbols
Arithmetic + - * / % ++ --
Assignment = += -= *= /= %=
Logical && || !
1.1.3 Keywords
1
Types keywords
1.1.4 Identifiers
An identifier is a string of letters ( a ~ z , A ~ Z , _ ) and digits ( 0 ~ 9 ) and it begins with a letter or underscore. Identifiers are
case-sensitive; for example, ident , Ident , and IDENT are not the same identifier. Note that keywords are not identifiers.
Floating-point literals: numbers that contain floating decimal points, such as 0.2 and 3.141 .
A string literal is a sequence of zero or more ASCII characters appearing between double-quote ( " ) delimiters. A double-quote
appearing with a string must be written after a " , e.g., "abc" , "Hello world" , and "She is a \"girl\"" .
1.2.1 Whitespace
1.2.2 Comments
C-style is texts surrounded by /* and */ delimiters, which may span more than one line;
C++ style comments are a text following a // delimiter running up to the end of the line.
Whichever comment style is encountered first remains in effect until the appropriate comment close is encountered. For
example,
// this is a comment // line */ /* with /* delimiters */ before the end
and
/* this is a comment // line with some and C delimiters */
are both valid comments.
The undefined characters or strings should be discarded by your scanner during parsing.
You will get 105pt if your scanner successfully generates the answers for all eleven programs. Otherwise, the mapping
between grade and correct count is listed below:
{"0":"0", "1":"30", "2":"50", "3":"60", "4":"70", "5":"75", "6":"80", "7":"85", "8":"90", "9":"95", "10":"100", "11":"105"}
Example: When passing only one test case, you will get 30pt; when passing eight test cases, you will get 90pt.
You can use the attached judge program to get the testing score by typing python3 judge/judge.py .
2
The output messages generated by your scanner must use the given names of token classes listed below.
* MUL ! NOT if IF
Input:
3
1 int a = 3;
2 int b = 0;
3 print(a);
4 b += 10;
5 // This is a comment
6 while (a < b){
7 /*
8 Multi-line comment
9 */
10 a++;
11 }
Output:
1 int INT
2 a IDENT
3 = ASSIGN
4 3 INT_LIT
5 ; SEMICOLON
6 int INT
7 b IDENT
8 = ASSIGN
9 0 INT_LIT
10 ; SEMICOLON
11 print PRINT
12 ( LPAREN
13 a IDENT
14 ) RPAREN
15 ; SEMICOLON
16 b IDENT
17 += ADD_ASSIGN
18 10 INT_LIT
19 ; SEMICOLON
20 // This is a comment C++ Comment
21 while WHILE
22 ( LPAREN
23 a IDENT
24 < LSS
25 b IDENT
26 ) RPAREN
27 { LBRACE
28 /* C Comment
29 Multi-line comment C Comment
30 */ C Comment
31 a IDENT
32 ++ INC
33 ; SEMICOLON
34 } RBRACE
35
36 Finish scanning,
37 total line: 12
38 comment line: 4
4
1 $ od -c answer/in05_comment.out
2 0000000 / * H e l l o W o r l d *
3 0000020 / \t C C o m m e n t \n / /
4 0000040 H e l l o W o r l d \t C
5 0000060 + + C o m m e n t \n / * \n *
6 0000100 H e l l o W o r l d \n * /
7 0000120 \t C C o m m e n t \n \n P a
8 0000140 r s e o v e r , t h e l i
9 0000160 n e n u m b e r i s 7 . \n
10 0000200 \n c o m m e n t : 5 l i n e
11 0000220 s \n \n
12 0000223
3. Environmental Setup
For Linux
Our grading system uses the Ubuntu environment. We will revise your uploaded code to adapt to our environment. In order to
facilitate the automated code revision process, we need your help to arrange your code in the following format as specified in
4. Submission.
4. Submission
Upload your homework to Moodle before the deadline.
Compress your files with zip or rar . Other file formats are not acceptable.
Your uploaded assignment must be organized illustrated below. Otherwise, our auto-grading tool will ignore it. Other
folders and files, e.g., answer/ , input/ , judge/ , etc. are optional, namely, uploading them or not is okay.
If our tool fails to recognize your homework files because you do not comply the rules, you will have to live demo your
homework with our TAs (after you realize your graded score is unacceptable low).
As for the filename of your compressed file, you should replace StudentID with your student id number. That is, your
compressed file should have the name like: Compiler_F12345678_HW1.zip .
Compiler_StudentID_HW1.zip/
└── Compiler_StudentID_HW1/
├── compiler_hw1.l
└── Makefile
5
5. References
Generating a lexical analyzer with the lex command: https://www.ibm.com/support/knowledgecenter/ssw_aix_71/genera
lprogramming/create_input_lang_lex_yacc.html
C Tokens, Keywords, Identifiers: https://www.guru99.com/c-tokens-keywords-identifier.html