1 / 14

Lex & Yacc

Lex & Yacc. COP 3402, Fall 2009. Lex – Symbol Table. When lexer has to recognize many tokens and their number is changing  use a symbol table stores lexemes and info about them, such as type of token

diane
Download Presentation

Lex & Yacc

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lex & Yacc COP 3402, Fall 2009

  2. Lex – Symbol Table • When lexer has to recognize many tokens and their number is changing  use a symbol table • stores lexemes and info about them, such as type of token • use a single pattern to match any word and then consult symbol table which token is found • Declaration start with type of token followed by the lexeme(s) • e.g:

  3. Lex – Symbol Table (Cont’d) • Symbol table (ST) maintenance routines and variables: • func. add_word(): adds a new lexeme into ST • func. lookup_word(): looks up a word already in ST • var. state: keeps track of whether we are looking up words (state =LOOKUP) or declaring them (state = token type)

  4. Lex Example -Definition Section

  5. Lex Example -Rules Section

  6. Lex Example –User Subroutines Section

  7. Lex Example –User Subroutines Section

  8. Lex Example - Test

  9. Yacc • For simple token recognition a lexer might be sufficient. • When specific sequences of tokens have to be recognized, a parser is necessary. • Grammar - a description of sequences of tokens and actions associated with them • e.g:

  10. Yacc • Provides a general tool for imposing structure on the input to a computer program • Takes a grammar specified by the user and writes a parser that recognizes valid “sentences” in that grammar • When lex scanner and yacc parser are used together, the parser is the higher level routine • parser calls lexeryylex() whenever it needs a token • lexer scans through input recognizing tokens

  11. Yacc • Lexer & parser have to decide on tokens codes

  12. Example – Lexer Part

  13. Example – Lexer Part Cont’d

  14. Example – Parser Part

More Related