CS 440/540 Examples in Java

    The purpose of this page is to provide you with some working examples of the use of the tools we will use in this class. As we move through the topics, you will want to get these files and try to build them yourself. You may use them as the starting point for the class projects, if you like.

    Similar examples in other languages:

    • C
    • C++

    To build the below examples using JFlex:

    java -jar ~white/JFlex.jar yourfile.l
    javac Yylex.java
    
    The first step creates the file Yylex.java (this name can be specified in the .l file), which is then compiled.


    The jar file for the most recent version of JFlex (1.5) is here (If you have any issues with the above, a copy of the Jar file for an older version of JFlex can be found here)
    A user's manual for Jflex is here


    Lexical Analysis

    • Remove white space (very simple example)
    • Character, line, word counting Lex example from the slides.
    • Lex example using states: removing comments from code
    • Transforming input


    Recursive Descent (LL) Parsing

    • Expression grammar recursive descent parsing
    • This is a nonsense grammar from a programming contest: the lowercase words are non-terminals, the uppercase characters are the terminals of this language.
        slurpy -> slimp slump
        slimp -> A rest_slimp 
        rest_slimp  -> H
      
        rest_slimp -> B slimp C
        rest_slimp -> slump C
        slump -> d_or_e F f_string end 
        d_or_e -> D
        d_or_e -> E
        f_string -> F f_string | lambda
        end -> slump
        end -> G 
      
       
      Some strings in the language: AHDFG, ADFGCDFFFFFG, ABAEFGCCDFEFFFFFFG
      Some strings NOT in the lanuage: AHDFGA, DFGAH, ABABCC
      • a Lex spec. for the language
      • a Makefile
      • Java parser.
    • A DFA based example.

    BYACC

    • We are using byacc/j for those students who want to write their parsers in Java. More infomation and some really good examples of the use of this version of the tool is available on this webpage. I've installed this tool on osf1
      /home/u1/white/byacc -J slurp2.y
      
      and on zeus
      /home/white/bin/byacc -J slurp2.y
      
      You can also download and install your own version if you like.
    • I/O: Whether or not your parser reads from standard input depends upon your initialization when you create it. If you wanted to read your input from a file where the name is given on the command line, you can do the following:
              public static void main (String [] args) throws IOException {
                      Parser yyparser = new Parser(new FileReader(args[0]));
                      yyparser.yyparse();
              }
      
      You can also read from standard input - this is the method I want you to use for assignments in this class.
              public static void main (String [] args) throws IOException {
                      Parser yyparser = new Parser(new InputStreamReader(System.in));
                      yyparser.yyparse();
              }
      

    LR Parsing

    An LR (YACC) version for the nonsense language:
      • a Lex spec. for the language (notice that it is different from the one above. I wanted to show you another way of passing single character tokens.
      • a Makefile for the program
      • the Yacc specification

    • A simple calculator
      • a Lex spec. for the language
      • the YACC specification
      • a Makefile for the program
    • A larger example based on a toy programming langauge.
      • a Lex spec. for the language
      • the YACC specification
      • a Makefile for the program


    Syntax directed translation

    • A simple calculator with evaluation
      • a Lex spec. for the language
      • the YACC specification
      • a Makefile for the program

    • A calculator with evaluation and with a symbol table. This example allows single character (lower case) variables to be given a value and later used in other expressions.
      • a Lex spec. for the language
      • the YACC specification
      • a Makefile for the program
    • Grid Example (using Semantic class)
      • lexer
      • parser
      • Makefile
    • Code generation Example (using Semantic class)
      • lexer
      • parser
      • Makefile
      Expects a single line from standard input such as: a := 2 + b + c
      for which it outputs spim:
              li $t0,2
              lw $t1,b
              lw $t2,c
              add $t1,$t1,$t2
              add $t0,$t0,$t1
              sw $t0,a