09CS303 Laboratory

download 09CS303 Laboratory

of 16

Transcript of 09CS303 Laboratory

  • 7/28/2019 09CS303 Laboratory

    1/16

    PES-INSTITUTE OF TECHNOLOGY

    DHEERAJ.D

    Asst.Prof

    Dept. of ISE, PESIT

    09 CS 303

    Compiler Design-Laboratory Manual.For 5th Semester students.

    By

    (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

  • 7/28/2019 09CS303 Laboratory

    2/16

    Compiler Design-Laboratory Manual.

    Dept of IS&E, PESIT

    Lex and Yacc

    1. Write a lex Program to identify a simple and a compound statement.%{

    #include

    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()

  • 7/28/2019 09CS303 Laboratory

    3/16

    Compiler Design-Laboratory Manual.

    Dept of IS&E, PESIT

    {

    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

    int flag=0;

    %}%%

    if|

    because|or|

    either|nor|

    neither|but{flag=1;}}%%

    main()

    {printf("Enter the sentence");

    yylex();

    if(flag)

  • 7/28/2019 09CS303 Laboratory

    4/16

    Compiler Design-Laboratory Manual.

    Dept of IS&E, PESIT

    {

    printf("compound");

    elseprintf("simple");

    }

    }2. Write a lex Program to count the number of keywords and identifiers in a sentence.%{

    #includestatic 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

    Void octtodeci(int);

    %}

    %%

    [0-7]+{octtodeci(atoi(yytext));}

    %%

    int main()

    {

    Printf(\n enter the octal number);

    Yylex();

    }

    Void octtodeci(int num)

    {

  • 7/28/2019 09CS303 Laboratory

    5/16

    Compiler Design-Laboratory Manual.

    Dept of IS&E, PESIT

    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

  • 7/28/2019 09CS303 Laboratory

    6/16

    Compiler Design-Laboratory Manual.

    Dept of IS&E, PESIT

    %%

    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

    #include

    %}%token ID

    %left '+','-'

    %left '*','/'

    %%

    E:E'+'E { printf("valid expression);}| E'-'E { printf("valid expression);}

  • 7/28/2019 09CS303 Laboratory

    7/16

    Compiler Design-Laboratory Manual.

    Dept of IS&E, PESIT

    | 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

    #include y.tab.h

  • 7/28/2019 09CS303 Laboratory

    8/16

    Compiler Design-Laboratory Manual.

    Dept of IS&E, PESIT

    %}

    %%

    [aA] return A;

    [bB] return B;

    .|\n return yytext[0];

    %%

    6.y

    /* 6.y yacc part */

    %{

    #include

    %}

    %token A B

    %%

    stmt: S \n {printf(\nValid expression); exit(1);}

    ;

    S: X B

    X: X A | AAAAAAAAAA

    %%

    main()

    {

    printf(\nEnter the expression: );

  • 7/28/2019 09CS303 Laboratory

    9/16

    Compiler Design-Laboratory Manual.

    Dept of IS&E, PESIT

    if(yyparse())

    {

    printf(\nValid expression);

    exit(0);

    }

    }

    int yyerror()

    {

    printf(\nInvalid expression\n);

    return 1;

    }

    int yywrap()

    {

    return 1;

    }

  • 7/28/2019 09CS303 Laboratory

    10/16

    Compiler Design-Laboratory Manual.

    Dept of IS&E, PESIT

    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. Thisis 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 emitinstructions 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.

  • 7/28/2019 09CS303 Laboratory

    11/16

    Compiler Design-Laboratory Manual.

    Dept of IS&E, PESIT

    How to install and use ANTLR?

    Download ANTLR 3.2, eclipse, JDK 1.6. Open eclipse ide.

    Select File

    New

    JPA 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 asoutput.

    If build fails, go to runrun asjava applicationokSelect Tool-org.antlrok, thisshould run your code.

  • 7/28/2019 09CS303 Laboratory

    12/16

    Compiler Design-Laboratory Manual.

    Dept of IS&E, PESIT

    1. Write an ANTLr grammar to accept the pascal statement READ(Value) and print aparse 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;

  • 7/28/2019 09CS303 Laboratory

    13/16

    Compiler Design-Laboratory Manual.

    Dept of IS&E, PESIT

    2. Write an ANTLr grammar to perform basic arithmetic operation in a calculatorgrammar 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;

  • 7/28/2019 09CS303 Laboratory

    14/16

    Compiler Design-Laboratory Manual.

    Dept of IS&E, PESIT

    3. Write an ANTLr grammar to accept a block of PASCAL statements between beginand 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)+)?;

  • 7/28/2019 09CS303 Laboratory

    15/16

    Compiler Design-Laboratory Manual.

    Dept of IS&E, PESIT

    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)+;

  • 7/28/2019 09CS303 Laboratory

    16/16

    Compiler Design-Laboratory Manual.

    Dept of IS&E, PESIT

    09 CS 303

    Week # Program # List of Programs

    1Instruction

    class.

    Introduction to the lab procedures and evaluation scheme, Lex and YACC

    specifications.

    2 Program #1 Write a lex Program to identify a simple and a compound statement.

    3 Program #2 Write a lex Program to count the number of keywords and identifiers in a sentence.

    4 Program #3 Write a lex program to convert an octal number to decimal number.

    5 Program #4Write a YACC Program to check whether given string a^nb^n is accepted by the

    grammar.

    6 Program #5 Write a YACC program to check the validity of an arithmetic expression.

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

    8Instruction

    classInstruction class on ANTLR (Another tool for Language Recognition).

    9 Program #1Write an ANTLr grammar to accept the pascal statement READ(Value) and print a

    parse tree for the same.

    10 Program #2 Write an ANTLr grammar to perform basic arithmetic operation in a calculator

    11 Program #3 Write an ANTLr grammar to accept a block of PASCAL statements between begin andend and print the parse tree for the same.

    12 Program #4 Write an ANTLr grammar to decide whether given sentence is simple or compound.

    Compiler Design-Laboratory Manual.