Y.N. Srikant
Department of Computer Science Indian Institute of Science Bangalore 560 012
NPTEL Course on Compiler Design
Y.N. Srikant
Compiler Overview
Outline of the Lecture
1 2 3 4 5
Compiler overview with block diagram Lexical analysis with LEX Parsing with YACC Semantic analysis with attribute grammars Intermediate code generation with syntax-directed translation Code optimization examples
6
Topics 5 and 6 will be covered in Part II of the lecture
Y.N. Srikant
Compiler Overview
Language Processing System
Y.N. Srikant
Compiler Overview
Compiler Overview
Y.N. Srikant
Compiler Overview
Translation Overview - Lexical Analysis
Y.N. Srikant
Compiler Overview
Lexical Analysis
LA can be generated automatically from regular expression specifications
LEX and Flex are two such tools
Tokens of the LA are the terminal symbols of the parser LA is usually called to deliver a token when the parser needs it Why is LA separate from parsing?
Simplification of design - software engineering reason I/O issues are limited LA alone LA based on finite automata are more efficient to implement than pushdown automata used for parsing (due to stack)
Y.N. Srikant
Compiler Overview
LEX Example
%% [A-Z]+ %% yywrap(){} main(){yylex();}
Y.N. Srikant
Compiler Overview
Form of a LEX File
LEX has a language for describing regular expressions It generates a pattern matcher for the REs described General structure of a LEX program {definitions} %% {rules} %% {user subroutines} A LEX compiler generates a C-program lex.yy.c as output
Y.N. Srikant
Compiler Overview
Definitions Section
Definitions Section contains definitions and included code
Definitions are like macros and have the following form: name translation digit [0-9] number {digit} {digit}* Included code is all code included between %{ and %} %{ float number; int count=0; %}
Y.N. Srikant
Compiler