comp - Learning Compiler Construction by Examples Jos´ e...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Learning Compiler Construction by Examples Jos´ e de Oliveira Guimar˜aes jose@dc.ufscar.br March 19, 2004 Contents 1 Compiler 1 3 2 Compiler 2 15 3 Compiler 3 19 4 Compiler 4 24 5 Compiler 5 28 6 Compiler 6 37 7 Compiler 7 42 8 Compiler 8 47 9 Compiler 9 60 10 Compiler 10 67 A The Complete Code of Compiler 6 1 B The Complete Code of Compiler 7 8 C The Complete Code of Compiler 8 17 D The Complete Code of Compiler 9 36 E The Complete Code of Compiler 10 69 1 This report is a small course on compiler construction. It explains how to build compilers using ten examples in Java. The first example uses a very small grammar described in three lines. The last one is a compiler for a language similar to Pascal. Each example, in general, adds some grammar rules to its predecessor. All the main elements of compiler construction are addressed in this report: lexical analysis, syntactical analysis, semantic analysis, abstract syntax tree (AST), code generation (to language C and assembly), symbol table, and code generation through the AST. Object-oriented programming is used extensively. In particular, the AST is composed by objects, which nicely organizes the compiler. 2 1 Compiler 1 We will build a compiler for the grammar Expr ::= ’(’ oper Expr Expr ’)’ | number oper ::= ’+’ | ’-’ number ::= ’0’ | ’1’ | ... | ’9’ In fact, we will code only a syntactical analyzer since there will be no code generation. Anyway, it will be called a compiler. Syntactic analysis is also called parsing and a syntactic analyzer is usually called parser. The compiler has two classes, Compiler and Main . The last one is the class of method main , where the program execution starts. In this method, the input to the compiler is given as an array of characters: public class Main { public static void main( String args ) { char input = "(- (+ 5 4) 1)".toCharArray(); Compiler compiler = new Compiler(); compiler.compile(input); } } The compiler itself is in class Compiler , which has method compile that compiles the input. In Compiler there are: • a method for each non-terminal of the grammar. See methods expr , oper , and number below; • a method error that prints an error message pointing where the error occurred; • a method nextToken which skips white spaces and puts the next character in instance variable token . nextToken is repeatedly called and token assumes ’(’ , ’-’ , ’(’ , ’+’ , ... Method nextToken is then the lexical analyzer. Instead of producing numbers representing the terminals, it produces the terminals themselves. This is only possible because all terminals are one- character tokens. Methods expr , oper , and number compose the parser (syntactical analyzer)....
View Full Document

This note was uploaded on 09/12/2011 for the course DPQ 09 taught by Professor Johncarpenter during the Spring '08 term at UFSCar.

Page1 / 197

comp - Learning Compiler Construction by Examples Jos´ e...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online