1 Pass Compiler 1 1.Introduction 1.1 Types of compilers 2.Stages of 1 Pass Compiler 2.1 Lexical analysis 2.2. syntactical analyzer 2.3. Code generation 3.Single pass compiling process 4.Comparison with multi pass 5.Advantages and limitations 1 Pass Compiler 2 What is a compiler ? Compiler is a computer program that translates a computer program written in 1 computer language into an equivalent program written in another computer language. Types of compilers 1.Single pass compiler 2.Multi-pass compiler 1 Pass Compiler 3 A single pass compiler makes a single pass over the source text, parsing, analyzing, and generating code all at once. A Multi pass compiler makes more than 1 pass over the source code ,producing intermediate forms of code after each stages, optimizing the program and generates object code. 1 Pass Compiler 4 1 lexical analysis: Lexical analysis involves scanning of source code and splitting it up into tokens 2 Syntactical analysis: syntactical analysis recognizes language constructs described by the grammar 3 Code generator generates the target language code 1 Pass Compiler 5 It is convenient to regard a source program as a sequence of tokens rather than simply string of characters. Token can be keyword, variable name, an integer, arithmetic operator etc. This task is called lexical analysis and part of compiler is called scanner, and input stream of source program is called the lexeme. For example an identifier may be defined like this: <indent>::=<letter>|<indent><letter>|<indent><digit> <letter> ::= a|b|c|d|…|z <digit> ::= 0|1|2|3|…|9 1 Pass Compiler 6 And the restriction in length of keywords, recognizing both single (‘R’,’E’,’A’,’D’) and multi character tokens (READ). Token specifier specify quality of a token like real no float no int type. Comments are ignored by scanners. Lexical analyzer can be easily visualized by using finite automata. Hence its representation will ease the implementation. Table representation is much clearer, so we use tables after constructing DFA. The tokens are accepted if it matches rules else error is reported. 1 Pass Compiler 7 After the token scan, each statement in the program must be recognized as some language construct, such as declaration or an assignment, described by grammar .This process is called syntax analysis or parsing. Syntactical analysis can be analyzed by constructing parse trees. 2 general classes of parsing techniques: Top-down :begins with the rule of the grammar that specifies goal of analysis, and attempt to construct the tree so terminals match statements being analyzed. Bottom-up :begins with the terminal node of the tree and attempts to combine this statements into successively higher level nodes until the root is reached. 1 Pass Compiler 8 Grammars are very much required if we consider construction of a compiler,The high level language is usually described in terms of grammar. This grammar specifies the form ,or syntax, of legal statements in the language. Then the problem of compilation becomes one of matching statements written by the programmer to structures defined by the grammar, and generating object code for each statement. 1 Pass Compiler 9 1 <prog> ::=PROGRAM<prog_name>Var<dec_list>BEGIN<stmt_list>end 2<prog-name> ::=id 3<dec-list>::=<id-list>:<type> 4<dec> ::=<id_list>:<type> 5<type>::=integer 6<id_list> ::=id|<id-list>,id 7<stmt-list>::=<stmt>|<stmt-list>;<stmt> 8<stmt> ::=<assign>|<read>|<write>|<for> 9<assign> ::=id :=<exp> 10 <exp> ::=<term>|<exp>+<term>|<exp>-<term> 11<term> ::=<factor>|<term>*<factor>|<term>div<factor> 12<factor> ::=id|int|(<exp>) 13<read> ::=READ(<id_list>) 14<write>::=WRITE(<id_list>) 15<for> ::=for<index-exp>do<body> 16<index-exp> ::=id :=<exp> to <exp> 17<body> ::=<stmt>|BEGIN<stmt-list> END 1 Pass Compiler 10 It is last process of compilation. It’s a process in compiler, which translates the lexed and parsed non-erroneous code into the required object code. It involves a set of routines called semantic routines or code generation routines, which generate the object code. The object code generation may depend on computer, but doesn't depend on parsing techniques. 1 Pass Compiler 11 Source program 1 Pass Compiler 12 First compiler calls the parser,which calls the lexer for getting the tokens. The lexer goes through the source program generating the tokens and return it to the parser. If parser requires some more tokens to recognize the tokens, it calls the lexer again. If parser gets the some unknown grammar constraint derivation it calls the error, and the object code is not generated. If parser gets some grammar valid constraints derivation then it calls the code generator and generates the object code. 1 Pass Compiler 13 So in single pass the compiler does not optimize the code. It directly converts it to source program. Single pass compiler no intermediate codes are generated. In multi pass assembler the step may continue to do intermediate code generation, machine independent code optimization Code generation Machine dependent code optimization And then machine code is generated. 1 Pass Compiler 14 In multi-pass the code is entirely lexed first and then parsed entirely, then may syntactically analyzed, and remaining steps are carried out. First step of compiler is calling lexer to convert entire source into tokens. IF there is forward reference that can be analyzed by scanning entire program more than once. That is not allowed in single pass assembler. 1 Pass Compiler 15 1 Pass Compiler 16 The single pass compiler required only 1 pass and no intermediate code step, so its quite efficient and fast in compiling. Computers running students jobs tend to spend a large amount of time performing compilation. The resulting object code is usually executed only once or twice. so, thus increases its significant benefits in system performance and job turn around time. 1 Pass Compiler 17 Pascal was explicitly designed to be easy to implement with a single pass compiler and hence to produce the code at faster. They have accomplished using the strict constructs of defining the variables to be defined before they are used. 1 Pass Compiler 18 The forward referencing problem. whenever a single pass is done we encounter a problem if any element that has to be processed not has been defined before. Then the single-pass compiler cant compile it and it may give error so next slide illustrates us with examples how Pascal program overcomes these, by defining strict rules. 1 Pass Compiler 19 Ex.2 Ex.1 var n:integer; procedure inc; begin n:=n+1 end procedure inc; begin n:=n+1 end; var n:integer; Here the example 1 is correct pascal program. 1 Pass Compiler 20 procedure ping(x:integer) begin ... pong(x-1);... end; procedure pong(x:integer) begin …ping(x–1);... end; Here there is problem of pong calling ping and ping calling pong. Which 1 to write 1st for single compiling!! 1 Pass Compiler 21 forward procedure pong(x:integer) procedure ping(x:integer) begin ... pong(x-1); ... end; procedure pong(x:integer) Begin ... ping(x–1); ... end; 1 Pass Compiler 22 If an program contains a forward reference, then it cant be compiled in single pass. If the variables are a mixture of Real and integer types 1 or more conversion of program operation will be need to be included in object code. Not all programs, some program due their language construct . ex:Hunter showed ALGOL 68 required at least 3 passes to compile. 1 Pass Compiler 23 Single compiler is very fast and are used when very fast compilation is required. The forward reference problem in 1 pass compiler can be solved by defining strict language constructs like in the pascal. Single pass compiler are better for large programs in general. Example of a platform where single pass can be used are: Pascal. 1 Pass Compiler 24 By Aditya.S.Hedge 1RV08CS009 Akhilesh kumar K C 1RV08CS010 1 Pass Compiler 25