Lex and Yacc Notes
1. Lex
Intro to lex/flex
- lex: lexical analyzer generator (詞法產生器)
- flex: fast lexical analyzer generator(快速詞法分析產生器)
- lex的開放原始碼版本
- flex > lex
- 也叫做 Scanners 或 lexers
Editor Plugin
- Visual Code:
Lex
https://github.com/textmate/lex-flex.tmbundle/
Compile
|
|
|
|
Usage
|
|
|
|
Solve yywrap() error
|
|
Common Lex Variables
- yyout
- The FILE* used for output, it is equivalent to stdout by default
- yyin
- The FILE* used for read input, it is equivalent to stdin by default
- yytext
- Store the matched token
- yyleng
- Store the length of the matched token
Common Lex Macros
- ECHO
- It is equivalent to
fprintf(yyout, “%s”, yytext)
- It is equivalent to
- BEGIN
- Switch the start state
- can do comments
2. Yacc
Intro to yacc/bison
- yacc: Yet Another Compiler Compiler
- bison: GNU bison
- bison > yacc
- 需與lex一起用
- 輸入是巴科斯範式(BNF)
- 採用LALR(1)
- Bottom up Parser
- Match到RHS(right hand side)會轉成LHS
- 也叫做Parser
Editor Plugin
- Visual Code:
Bison & Yacc
https://marketplace.visualstudio.com/items?itemName=se2dev.vscode-yacc
Compile
|
|
Usage
|
|
|
|
Rules section
- Context Free Grammer
C Code section
- yyerror
- deal with error . ex: sytax error
- yyparse
- Use the productions of rule section to analyze the syntax
- Communication with yylex
Symbol Attributes
- yylval:
- This is a variable defined in *.y file
- When token is returned to yacc , yacc can use it to do something
- By default, yylval is of type int
Non-Integer Symbol Attributes
- The %union directive
- Declare data type for terminals and non-terminals
- terminals: %token
token … - non-terminals: %type
non-terminal …
- terminals: %token
|
|
Token Declarations
- %token: generic token declarations
- %left: left-associative binary operators with precedence
- ex: 9-3-2 = 4 not 8
- %right: right-associative binary operators with precedence
- ex: 2 ^ 2 ^ 3 = 256 not 64
- %nonassoc: non-associative tokens with precedence (從左邊算到右邊跟右算到左一樣)