-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathreadme
More file actions
37 lines (32 loc) · 1.34 KB
/
Copy pathreadme
File metadata and controls
37 lines (32 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
Simple C Compiler
The four main stages in compiler design :
Lexical Analysis - The input program is broken down into tokens (identifiers, operators, numbers, keywords, punctuators). A symbol table is created with the list of tokens obtained from the input. Lexical Analysis is done using Flex tool to generate tokens.
To install Lex :
sudo apt-get install flex
Execution :
lex prog.l
cc lex.yy.c -ll
./a.out
cat output.txt
Syntax Analysis - In this step, the tokens generated after lexical analysis is examined to construct syntactically correct sentences and the syntax errors present are printed out (missing parenthesis, semicolon). Syntax Analysis is performed using Yacc tool as a parser.
To install Yacc :
sudo apt-get install bison
sudo apt-get install byacc
Execution :
lex prog.l
yacc prog.y
cc y.tab.c -ll -ly
./a.out
Semantic Analysis - The parsed output generated in the above step is examined for semantic errors and the abstract syntax tree is printed. Semantic errors like type checking, undeclared variables, multiple declarations of variables are verified.
Execution :
lex prog.l
yacc prog.y
cc y.tab.c -ll -ly
./a.out
cat out.txt
lex prog.l
yacc absprog.y
cc y.tab.c -ll -ly
./a.out
cat output.txt
Code Generation - Using the abstract syntax tree we need to generate the intermediate code in three address code format.