Professional Documents
Culture Documents
09 CS 303
(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. { 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)
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; }
10
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;
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
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)+)?;
14
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)+;
15
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.
16