02-01parsing - Reading from files rather than standard...

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

View Full Document Right Arrow Icon
1 CSE 450: Compilers K. Stirewalt Final details on lex… CSE 450: Compilers K. Stirewalt Reading from files rather than standard input Lex-generated lexers read from standard input by default, but they may be adapted to input from a file (or from multiple files) Global variable: yyin – Of type FILE* – Defaults to stdin – May point to file opened via fopen() CSE 450: Compilers K. Stirewalt End of file and yywrap() When lex-generated lexers reach end of file on yyin , they invoke yywrap to decide what to do next – Default implementation returns 1 – You may provide your own implementation, which reassigns yyin to some additional file and returns 0 (1 if no more files to process) CSE 450: Compilers K. Stirewalt Producing a stream of tokens Conceptually, a lexer produces a stream of tokens, which is then consumed (during syntax analysis) by a parser Lexer need not run through all of the input before parsing begins In most compilers, parser invokes lexer repeatedly, requesting one token at a time CSE 450: Compilers K. Stirewalt Making yylex() return a token Actions in lex files may contain arbitrary C++ code, including return statements –Execu t ing return from within an action causes yylex to return to its caller t return 10; from within an action causes yylex to return the integer 10 Strategy: Define integer constants that identify token types and return those constants when matching a token of a given type CSE 450: Compilers K. Stirewalt Getting the lexeme too. .. Lex-generated lexers provide a global variable yytext , which holds the text of the most recently matched token Important: – yytext overwritten on each call to yylex – so if the lexeme is needed for further analysis in later phases of compilation, it will need to be stored immediately
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 CSE 450: Compilers K. Stirewalt Syntactic analysis: Part I: Foundational theory Topics: Parsing and context- free grammars Parse trees and ambiguity CSE 450: Compilers K. Stirewalt Structure of a Compiler Source Language Target Language Semantic Analyzer Syntax Analyzer Lexical Analyzer Front End Code Optimizer Target Code Generator Back End Int. Code Generator Intermediate Code CSE 450: Compilers K. Stirewalt Today! Source Language Target Language Semantic Analyzer Syntax Analyzer Lexical Analyzer Front End Code Optimizer Target Code Generator Back End Int. Code Generator Intermediate Code CSE 450: Compilers K. Stirewalt A closer look at syntax analysis Lexical Analysis or Scanner if (idx == 0) idx = 750; if ( idx == 0 ) idx = 750 ; Syntax Analysis or Parsing tree depicting syntactic structure of input if IsEq Assign idx 0 idx 750 stream of tokens CSE 450: Compilers K. Stirewalt Parsing Defn:
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 07/25/2008 for the course CSE 450 taught by Professor Stirewalt during the Spring '08 term at Michigan State University.

Page1 / 6

02-01parsing - Reading from files rather than standard...

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

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