This preview shows page 1-2-3-4-5-6-7-8-9-63-64-65-66-67-68-69-70-71-127-128-129-130-131-132-133-134-135 out of 135 pages.
61A Lecture 27November 2, 2011Wednesday, November 2, 2011ParsingA Parser takes as input a string that contains an expression and returns an expression tree2Wednesday, November 2, 2011ParsingA Parser takes as input a string that contains an expression and returns an expression tree2stringparserexpression treeEvaluatorvalueWednesday, November 2, 2011ParsingA Parser takes as input a string that contains an expression and returns an expression tree2stringparserexpression treeEvaluatorvalue'add(2, 2)'Wednesday, November 2, 2011ParsingA Parser takes as input a string that contains an expression and returns an expression tree2stringparserexpression treeEvaluatorvalue'add(2, 2)' Exp('add', [2, 2])Wednesday, November 2, 2011ParsingA Parser takes as input a string that contains an expression and returns an expression tree2stringparserexpression treeEvaluatorvalue'add(2, 2)' Exp('add', [2, 2]) 4Wednesday, November 2, 2011ParsingA Parser takes as input a string that contains an expression and returns an expression tree2stringparserexpression treeEvaluatorvalue'add(2, 2)' Exp('add', [2, 2]) 4Wednesday, November 2, 2011ParsingA Parser takes as input a string that contains an expression and returns an expression tree2stringparserexpression treeEvaluatorvalue'add(2, 2)' Exp('add', [2, 2]) 4EvalWednesday, November 2, 2011ParsingA Parser takes as input a string that contains an expression and returns an expression tree2stringparserexpression treeEvaluatorvalue'add(2, 2)' Exp('add', [2, 2]) 4EvalApplyWednesday, November 2, 2011ParsingA Parser takes as input a string that contains an expression and returns an expression tree2stringparserexpression treeEvaluatorvalue'add(2, 2)' Exp('add', [2, 2]) 4EvalApplyApply a function to its argumentsWednesday, November 2, 2011ParsingA Parser takes as input a string that contains an expression and returns an expression tree2stringparserexpression treeEvaluatorvalue'add(2, 2)' Exp('add', [2, 2]) 4EvalApplyApply a function to its argumentsWednesday, November 2, 2011ParsingA Parser takes as input a string that contains an expression and returns an expression tree2stringparserexpression treeEvaluatorvalue'add(2, 2)' Exp('add', [2, 2]) 4EvalApplyEvaluate operandsApply a function to its argumentsWednesday, November 2, 2011ParsingA Parser takes as input a string that contains an expression and returns an expression tree2stringparserexpression treeEvaluatorvalue'add(2, 2)' Exp('add', [2, 2]) 4EvalApplyEvaluate operandsApply a function to its argumentsWednesday, November 2, 2011ParsingA Parser takes as input a string that contains an expression and returns an expression tree2stringparserexpression treeEvaluatorvalue'add(2, 2)' Exp('add', [2, 2]) 4EvalApplyEvaluate operandsApply a function to its argumentsLexical analysisWednesday, November 2, 2011ParsingA Parser takes as input a string that contains an expression and returns an expression tree2stringparserexpression treeEvaluatorvalue'add(2, 2)' Exp('add', [2, 2]) 4EvalApplyEvaluate operandsApply a function to its argumentsLexical analysisSyntactic analysisWednesday, November 2, 2011Two-Stage Parsing3Wednesday, November 2, 2011Two-Stage ParsingLexical analyzer: Analyzes an input string as a sequence of tokens, which are symbols and delimiters3Wednesday, November 2, 2011Two-Stage ParsingLexical analyzer: Analyzes an input string as a sequence of tokens, which are symbols and delimitersSyntactic analyzer: Analyzes a sequence of tokens as an expression tree, which typically includes call expressions3Wednesday, November 2, 2011Two-Stage ParsingLexical analyzer: Analyzes an input string as a sequence of tokens, which are symbols and delimitersSyntactic analyzer: Analyzes a sequence of tokens as an expression tree, which typically includes call expressions3def calc_parse(line):Wednesday, November 2, 2011Two-Stage ParsingLexical analyzer: Analyzes an input string as a sequence of tokens, which are symbols and delimitersSyntactic analyzer: Analyzes a sequence of tokens as an expression tree, which typically includes call expressions3def calc_parse(line): """Parse a line of calculator input."""Wednesday, November 2, 2011Two-Stage ParsingLexical analyzer: Analyzes an input string as a sequence of tokens, which are symbols and delimitersSyntactic analyzer: Analyzes a sequence of tokens as an expression tree, which typically includes call expressions3def calc_parse(line): """Parse a line of calculator input.""" tokens = tokenize(line)Wednesday, November 2, 2011Two-Stage ParsingLexical analyzer: Analyzes an input string as a sequence of tokens, which are symbols and delimitersSyntactic analyzer: Analyzes a sequence of tokens as an expression tree, which typically includes call expressions3def calc_parse(line): """Parse a line of calculator input.""" tokens = tokenize(line)Lexical analysis is also called tokenizationWednesday, November 2, 2011Two-Stage ParsingLexical analyzer: Analyzes an input string as a sequence of tokens, which are symbols and delimitersSyntactic analyzer: Analyzes a sequence of tokens as an expression tree, which typically includes call expressions3def calc_parse(line): """Parse a line of calculator input.""" tokens = tokenize(line) expression_tree = analyze(tokens)Lexical analysis is also called tokenizationWednesday, November 2, 2011Parsing with Local StateLexical analyzer: Creates a list of tokensSyntactic analyzer: Consumes a list of tokens4Lexical analysis is also called tokenizationWednesday, November 2, 2011Parsing with Local StateLexical analyzer: Creates a list of tokensSyntactic analyzer: Consumes a list of tokens4def calc_parse(line): """Parse a line of calculator input.""" tokens = tokenize(line) expression_tree = analyze(tokens)Lexical analysis is also called tokenizationWednesday, November 2, 2011Parsing with Local StateLexical analyzer: Creates a list of tokensSyntactic analyzer: Consumes a list of tokens4def calc_parse(line): """Parse a line of calculator input.""" tokens = tokenize(line) expression_tree = analyze(tokens) if len(tokens) > 0: raise SyntaxError('Extra token(s)')Lexical analysis is also called tokenizationWednesday, November 2, 2011Parsing with Local StateLexical analyzer: Creates a list of tokensSyntactic analyzer: Consumes a list of tokens4def calc_parse(line): """Parse a line of calculator input.""" tokens = tokenize(line) expression_tree = analyze(tokens) if len(tokens) > 0: raise SyntaxError('Extra
View Full Document