Lex yacc
This presentation is the property of its rightful owner.
Sponsored Links
1 / 14

Lex & Yacc PowerPoint PPT Presentation


  • 148 Views
  • Uploaded on
  • Presentation posted in: General

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

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Lex yacc

Lex & Yacc

COP 3402, Fall 2009


Lex symbol table

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:


Lex symbol table cont d

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)


Lex example definition section

Lex Example -Definition Section


Lex example rules section

Lex Example -Rules Section


Lex example user subroutines section

Lex Example –User Subroutines Section


Lex example user subroutines section1

Lex Example –User Subroutines Section


Lex example test

Lex Example - Test


Lex yacc

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:


Lex yacc

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


Lex yacc

Yacc

  • Lexer & parser have to decide on tokens codes


Example lexer part

Example – Lexer Part


Example lexer part cont d

Example – Lexer Part Cont’d


Example parser part

Example – Parser Part


  • Login