Syntax AnalysisPart IChapter 4
2Position of a Parser in theCompiler ModelToken,ParsertokenvalSourceLexicalIntermediateand rest ofProgramAnalyzerrepresentationfront-endGetnext1tokenLexical errorSyntax errorSemantic eirorSymbol Table
3The ParserAparserimplementsaC-FgrammarThe role of the parser is twofold:1.To check syntax (= string recognizer)Andtoreportsyntaxerrorsaccurately2.To invoke semantic actionsFor static semantics checking, e.g.type checking ofexpressions,functions,et.For syntax-directed translation of the source code toan intermediate representation
4Syntax-Directed Translation.One of the major roles of the parser is toproducean intermediate representation (IR) of the sourceprogram using syntax-directed translation methods.PossibleIRoutput:-Abstractsyntaxtrees(ASTs)-Control-flowgraphs(CFGs)withtriples,three-addresscode,orregistertransferlistnotation-WHIRL(SGIPro64compiler)has5IRlevels!
5Error HandlingAgood compiler should assist in identifying andlocating errors-Lexical errors:important, compiler can easily recoverand continue-Syntax errors: most important for compiler, can almostalways recover-Static semanticerrors:important, can sometimesrecover-Dynamicsemanticerrors:hard orimpossibletodetectatcompiletime,runtimechecks arerequired-Logicalerrors:hard orimpossibletodetect