You are on page 1of 16

PES-INSTITUTE OF TECHNOLOGY

09 CS 303

Compiler DesignLaboratory Manual.


For 5th Semester students.
By

DHEERAJ.D Asst.Prof Dept. of ISE, PESIT

(Autonomous Institute under VTU , Belgaum and UGC , New Delhi)100 feet ring road, BSK 3rd stage, Banglore-560 085.Phone: 080-2672 1983, Fax: 080-2672 0886. http://pesit.pes.edu

Compiler Design-Laboratory Manual.

Lex and Yacc


1. Write a lex Program to identify a simple and a compound statement. %{ #include<stdio.h> int F0=0,F1=0,F2=0,error=0,l1=0,l2=0; %} verb am|run|sit|did|study|is|large|go|come subject [a-zA-Z]+ compnd and|but|also|either|neither|yet|still|consequences %% {verb} { if(F2==1) l2=1; F2=1; if(F1==0) error=1; } {compnd} { F0=1; } {subject} { if(F1!=0) l1=1; F1++; } %% main()

Dept of IS&E, PESIT

Compiler Design-Laboratory Manual. { printf(\n Enter a sentence: ); yylex(); if(error==1 || F2==0 || F1==0) { printf(\n Invalid sentence); exit(0); } if(F0==1 && l1==1 && l2==1) printf(\n Compound sentence\n); else printf( \nSimple sentence\n); }

Aliter %{ #include<stdio.h> int flag=0; %} %% if| because| or| either| nor| neither| but{flag=1;}} %% main() { printf("Enter the sentence"); yylex(); if(flag)
Dept of IS&E, PESIT 3

Compiler Design-Laboratory Manual. { printf("compound"); else printf("simple"); } } 2. Write a lex Program to count the number of keywords and identifiers in a sentence. %{ #include<stdio.h> static int key_word=0; static int identifier=0; %} %% "include"|"for"|"define" {key_word++;printf("keyword found");} "int"|"char"|"float"|"double" {identifier++;printf("identifier found");} %% int main() { printf("enter the sentence"); yylex(); printf("keyword are: %d\n and identifier are: %d\n",key_word,identifier); }

3. Write a lex program to convert an octal number to decimal number. %{ #include<stdio.h> Void octtodeci(int); %} %% [0-7]+{octtodeci(atoi(yytext));} %% int main() { Printf(\n enter the octal number); Yylex(); } Void octtodeci(int num) {
Dept of IS&E, PESIT 4

Compiler Design-Laboratory Manual. int sum=0; int r=0; int i=0; while(num!=0) { r=num%10; sum=sum+r*po(8,i); i++; num=num/10; } Printf(\n the decimal equivalent is %d,sum); } int po(int a , int m) { int j=0; int k=1; for(j=0;j<m;j++) { k=k*a; } Return k; } 4. Write a YACC Program to check whether given string a^nb^n is accepted by the grammar. %{ #include<stdio.h> %} %token A B %% S : ASB | AB | ;
Dept of IS&E, PESIT 5

Compiler Design-Laboratory Manual. %% yyerror() { Printf(\n the string does not belong to the grammar); } int yylex() { char ch; ch=getchar(); if (ch==a) return A; else if (ch==b) return B; else if(ch==\n) return 0; else return ch; } int main() { Printf(enter the expression \n); yyparse(); Printf(\n string accepted by grammar); Return 0; } 5. Write a YACC program to check the validity of an arithmetic expression.

%{ #include<stdio.h> #include<stdlib.h> %} %token ID %left '+','-' %left '*','/' %% E:E'+'E { printf("valid expression);} | E'-'E { printf("valid expression);}
Dept of IS&E, PESIT 6

Compiler Design-Laboratory Manual. | E'/'E { printf("valid expression);} | E'*'E { printf("valid expression);} | ID ; %% int yylex() { Char ch; Ch=getchar(); if(isalnum(ch)) return ID; else if(ch==\n) return 0; else return ch; }

yyerror() { Printf(\n invalid expression); } int main() { printf("Enter the expression"); yyparse(); }

6. Write a YACC Program to identify an input for the grammar a^nb (n>=10)

/* 6.l lex part*/ %{ #include<stdio.h> #include y.tab.h


Dept of IS&E, PESIT 7

Compiler Design-Laboratory Manual. %} %% [aA] return A; [bB] return B; .|\n return yytext[0]; %%

6.y

/* 6.y yacc part */ %{ #include<stdio.h> %} %token A B %% stmt: S \n {printf(\nValid expression); exit(1);} ; S: X B X: X A | AAAAAAAAAA %% main() { printf(\nEnter the expression: );
Dept of IS&E, PESIT 8

Compiler Design-Laboratory Manual. if(yyparse()) { printf(\nValid expression); exit(0); } } int yyerror() { printf(\nInvalid expression\n); return 1; }

int yywrap() { return 1; }

Dept of IS&E, PESIT

Compiler Design-Laboratory Manual.

ANTLR(Another tool for Language Recognition).


In computer-based language recognition, ANTLR (pronounced Antler), or Another Tool for Language Recognition, is a parser generator that uses LL (*) parsing. ANTLR is the successor to the Purdue Compiler Construction Tool Set (PCCTS), first developed in 1989, and is under active development. Its maintainer is Professor Terence Parr of the University of San Francisco. ANTLR takes as input a grammar that specifies a language and generates as output source code for a recognizer for that language. At the moment, ANTLR supports generating code in the programming languages Ada95, Action Script, C, C#, Java, JavaScript, Objective-C, Perl, Python, and Ruby. A language is specified using a context-free grammar which is expressed using Extended Backus Naur Form EBNF. ANTLR allows generating parsers, lexers, tree parsers, and combined lexer-parsers. Parsers can automatically generate abstract syntax trees which can be further processed with tree parsers. ANTLR provides a single consistent notation for specifying lexers, parsers, and tree parsers. This is in contrast with other parser/lexer generators and adds greatly to the tool's ease of use. By default, ANTLR reads a grammar and generates a recognizer for the language defined by the grammar (i.e. a program that reads an input stream and generates an error if the input stream does not conform to the syntax specified by the grammar). If there are no syntax errors, then the default action is to simply exit without printing any message. In order to do something useful with the language, actions can be attached to grammar elements in the grammar. These actions are written in the programming language in which the recognizer is being generated. When the recognizer is being generated, the actions are embedded in the source code of the recognizer at the appropriate points. Actions can be used to build and check symbol tables and to emit instructions in a target language, in the case of a compiler. As well as lexers and parsers, ANTLR can be used to generate tree parsers. These are recognizers that process abstract syntax trees which can be automatically generated by parsers. These tree parsers are unique to ANTLR and greatly simplify the processing of abstract syntax trees.

Dept of IS&E, PESIT

10

Compiler Design-Laboratory Manual.

How to install and use ANTLR?


Download ANTLR 3.2, eclipse, JDK 1.6. Open eclipse ide. Select FileNewJPA Project. Enter container name, project name, ok or finish. Select FileNewjava project. Enter a project nameok. If there is a prompt that asks for build nature press ok. Right click on the project in the package explorer. NewotherANTLRcombined grammarnextenter grammar name (grammar name = class name)ok. The ide generates a .g file for you. Type the program in the text editor. Right click on the project in the package explorer. SelectconfigureANTLR project. Right click on the project again in the package explorer. Select Build pathadd external archives. Select the directory where the ANTLR jar file resides. Note: if there are no errors in your program, ANTLR generates a lexer and a parser file for you. Go to the interpreter, give the input for the grammar, ANTLR generates a parse tree as output. If build fails, go to runrun asjava applicationokSelect Tool-org.antlrok, this should run your code.

Dept of IS&E, PESIT

11

Compiler Design-Laboratory Manual. 1. Write an ANTLr grammar to accept the pascal statement READ(Value) and print a parse tree for the same.

grammar Read; options { language = Java; } tokens{ READ = 'READ'; OB = '('; CB = ')'; CM = ','; } @members { /*public static void main (String [] args) throws Exception { ReadLexer lex = new ReadLexer(new ANTLRFileStream(args[0])); CommonTokenStream tokens = new CommonTokenStream(lex); ReadParser parser = new ReadParser(tokens); try { parser.expr(); } catch(RecognitionException e) { e.printStackTrace(); } }*/ }

IDLIST : ID(((CM)ID)+)?; fragment ID : AL+; fragment AL : 'a'..'z'|'A'..'Z'; read: READ OB IDLIST CB;

Dept of IS&E, PESIT

12

Compiler Design-Laboratory Manual. 2. Write an ANTLr grammar to perform basic arithmetic operation in a calculator grammar Test; options { language = Java; } tokens { PLUS = '+'; MINUS = '-'; MULT = '*'; DIV = '/'; ASSIGNMENT =':='; } @members { public static void main(String[] args) throws Exception { TestLexer lex = new TestCalLexer(new ANTLRFileStream(args[0])); CommonTokenStream tokens = new CommonTokenStream(lex); TestParser parser = new TestParser(tokens); try { parser.expr(); } catch(RecognitionException e) { e.printStackTrace(); } } } /*Lexer Rules*/ ID: A+; NUMBER: (DIGIT) + ('.' (DIGIT)+)? ; WS: ('\t'|' '|'\r'|'\n')+ ; fragment A : 'a'..'z'|'A'..'Z'; /*fragment:Special construct in ANTLR to describe a part of another rule*/ fragment DIGIT: '0'..'9' ; /*Parser Rules*/ stmt: ID ASSIGNMENT expr; expr:term((PLUS|MINUS)term)*; term:factor((MULT|DIV)factor)*; factor: ID | NUMBER;
Dept of IS&E, PESIT 13

Compiler Design-Laboratory Manual.

3. Write an ANTLr grammar to accept a block of PASCAL statements between begin and end and print the parse tree for the same. grammar Prog1; options { language = Java; } tokens { ASSIGN = ':='; PLUS = '+'; MINUS = '-'; MULT = '*'; DIV = '/'; TO = 'TO'; DO = 'DO'; BEGIN = 'BEGIN'; END = 'END'; WRITE = 'WRITE'; SEMICLN = ';'; FOR = 'FOR'; OB = '('; CB = ')'; CM = ','; }

ID: A+; NUMBER : (DIGIT)+('.' (DIGIT)+)? ; //WS: ('\t'|' '|'\r'|'\n')+ ; fragment A : 'a'..'z'|'A'..'Z'; fragment DIGIT: '0'..'9'; for: FOR ID ASSIGN NUMBER TO NUMBER DO BEGIN stmtlist END; stmtlist: stmt+; stmt: write | ID ASSIGN expr SEMICLN; expr:term((PLUS|MINUS)term)* ; term:factor((MULT|DIV)factor)*; factor: ID | NUMBER; write : WRITE OB idlist CB SEMICLN; idlist : ID((CM ID)+)?;

Dept of IS&E, PESIT

14

Compiler Design-Laboratory Manual.

4. Write an ANTLr grammar to decide whether given sentence is simple or compound. grammar Prog2; options { language = Java; } VERBS : 'doing' | 'using'; NOUN : 'bangalore' | 'dog'; ADJECTIVE : 'complicated'|'massive'; sentence : (VERBS|NOUN|ADJECTIVE)+;

Dept of IS&E, PESIT

15

Compiler Design-Laboratory Manual.

09 CS 303
Compiler Design-Laboratory Manual.

Week # Program #
1 2 3 4 5 6 7 8 9 10 11 12 Instruction class. Program #1 Program #2 Program #3 Program #4 Program #5 Program #6 Instruction class Program #1 Program #2 Program #3 Program #4

List of Programs
Introduction to the lab procedures and evaluation scheme, Lex and YACC specifications. Write a lex Program to identify a simple and a compound statement. Write a lex Program to count the number of keywords and identifiers in a sentence. Write a lex program to convert an octal number to decimal number. Write a YACC Program to check whether given string a^nb^n is accepted by the grammar. Write a YACC program to check the validity of an arithmetic expression. Write a YACC Program to identify an input for the grammar a^nb (n>=10). Instruction class on ANTLR (Another tool for Language Recognition). Write an ANTLr grammar to accept the pascal statement READ(Value) and print a parse tree for the same. Write an ANTLr grammar to perform basic arithmetic operation in a calculator Write an ANTLr grammar to accept a block of PASCAL statements between begin and end and print the parse tree for the same. Write an ANTLr grammar to decide whether given sentence is simple or compound.

Dept of IS&E, PESIT

16

You might also like