An Overview of a Compiler

Only available on StudyMode
  • Topic: Parsing, Compiler, Formal grammar
  • Pages : 8 (1494 words )
  • Download(s) : 71
  • Published : February 25, 2013
Open Document
Text Preview
An Overview of a Compiler - Part 1
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 Overview

Rules Section
Contains patterns and C-code A line starting with white space or material enclosed in %{ and %} is C-code A line starting with anything else is a pattern line Pattern lines contain a pattern followed by some white space and C-code {pattern} {action (C − code)} C-code lines are copied verbatim to the the generated C-file Patterns are translated into NFA which are then converted into DFA, optimized, and stored in the form of a table and a driver routine The action associated with a pattern is executed when the DFA recognizes a string corresponding to that pattern and reaches a final state Y.N. Srikant Compiler Overview

LEX Example 1

number [0-9]+\.?|[0-9]*\.[0-9]+ name [A-Za-z][A-Za-z0-9]* %% [ ] {/* skip blanks */} {number} {sscanf(yytext,"%lf",&yylval.dval); return NUMBER;} {name} {struct symtab *sp =symlook(yytext); yylval.symp = sp; return NAME;} "++" {return POSTPLUS;} "--" {return POSTMINUS;} "$" {return 0;} \n|. {return yytext[0];}

Y.N. Srikant

Compiler Overview

LEX Example 2
%{ FILE *declfile; %} blanks [ \t]* letter [a-z] digit [0-9] id ({letter}|_)({letter}|{digit}|_)* number {digit}+ arraydeclpart {id}"["{number}"]" declpart ({arraydeclpart}|{id}) decllist ({declpart}{blanks}","{blanks})* {blanks}{declpart}{blanks} declaration (("int")|("float")){blanks} {decllist}{blanks}; Y.N. Srikant Compiler Overview

LEX Example (contd.)
%% {declaration} fprintf(declfile,"%s\n",yytext); %% yywrap(){ fclose(declfile); } main(){ declfile = fopen("declfile","w"); yylex(); } Examples of declarations: int a, b[10], c, d[25]; float k[20], l[10], m,n; Y.N. Srikant Compiler Overview

Translation Overview - Syntax Analysis

Y.N. Srikant

Compiler Overview

Parsing or Syntax Analysis
Syntax analyzers (parsers) can be generated automatically from several variants of context-free grammar specifications LL(1), and LALR(1) are the most popular ones ANTLR (for LL(1)), YACC and Bison (for LALR(1)) are such tools

Parsers are...
tracking img