main ADD int x10 y20 sum sumxy 1112022
- Slides: 120
main( )/*ADD*/ {int x=10, y=20, sum; sum=x+y; } 1/11/2022 编译原理 词法分析 main、(、)、{、int、x 、=、10、, 、y、=、 20、, 、sum、; 、sum、 =、x、+、y、; 、} Tuesday, January 11, 2022 5
【例3. 5】G[S]: S→a. A|a A→d. A|d 1/11/2022 编译原理 文法产生式 正规式 规则 1 A→x. B, B→y A=xy 规则 2 A→x. A|y A=x*y 规则 3 A→x, A→y A=x|y S=a. A|a A= d*d 代入: S=ad*d|a= ad* Tuesday, January 11, 2022 28
【例3. 6】求正规式(a|b)(a|b|0|1)*对应的正规文法 S→(a|b)A S→(a|b)(a|b|0|1)* A→a. A|b. A|0 A|1 A|ε G[S]: S→a. A|b. A A→a. A|b. A|0 A|1 A|ε 1/11/2022 编译原理 Tuesday, January 11, 2022 30
能接受 000 111 1010001 110000001 不能接受 00 01100 (0|1)*(000|111)(0|1) 1/11/2022 编译原理 Tuesday, January 11, 2022 * 44
画出能够识别C语言注释/* */的DFA others 1 / * 2 others 3 * * 4 others / 5 all 6 all 1/11/2022 编译原理 Tuesday, January 11, 2022 45
Ø (2)状态合并 a 1 c 0 3 a 1, 2 c 3 b 2 b (a) 1/11/2022 0 a 编译原理 (b) Tuesday, January 11, 2022 52
课堂练习 start 1/11/2022 编译原理 i a 1 2 b a 3 a 5 b 4 b Tuesday, January 11, 2022 a 6 f b 61
等价的DFA a a A a b C E a start S a b b a b B b D a b F b 1/11/2022 编译原理 Tuesday, January 11, 2022 62
1 2 3 4 a 6 7 1 4 b 区号 3 1 3 5 2 6 3 1 2 3 4 a 6 7 1 4 5 6 7 7 4 4 3 1 2 5 6 7 7 4 4 1/11/2022 编译原理 4 b 区号 3 1 3 5 2 6 3 4 3 1 5 2 Tuesday, January 11, 2022 68
(2)消除M中的所有结点 a|b x ε 4 aa a|b ε 0 y bb 2 x ε ε aa(a|b) * 0 y bb(a|b) * a|b a, b 解: (1)加xy a, b a 3 x ε 4 ε(a|b)*(aa|bb)(a|b)* x y y ε 0 b 1 1/11/2022 a 编译原理 b 2 a, b Tuesday, January 11, 2022 73
若s, t为Σ上的正规式 (a)对于正规式R=st x st y (b)对于正规式R=s|t x y x x 1/11/2022 y 编译原理 t t y s x y t (c)对于正规式R=rs* t rs*t s x r s t t Tuesday, January 11, 2022 y 75
例: 为R=(a|b)* abb构造NFA, 使得L(N)=L(R) S (a|b) * A abb Z a|b S a A bb Z a S a A b B b Z b 1/11/2022 编译原理 Tuesday, January 11, 2022 76
例: 求与文法G[S]等价的NFA G[S]: S→a. A|b. B|ε A→a. B|b. A B→a. S|b. A|ε b A a 求得: a b b S a ε 1/11/2022 编译原理 Z Tuesday, January 11, 2022 B ε 81
转换方法 正规文法 6 1 5 2 3 NFA 7 正规式 4 DFA 8 最小化 1/11/2022 编译原理 Tuesday, January 11, 2022 82
例:标识符: digit [0 -9] letter [A-Za-z] id ({letter}|[_])({letter}|{digit}|[_])* 带符号整数: integer signinteger digit (digit)* +| - |ε sign integer 说明部分可用一个名字代表一个正规式,增 加程序的可读性 1/11/2022 编译原理 Tuesday, January 11, 2022 87
下面是识别C语言部分单词符号的LEX源程序: /*说明部分*/ digit [0 -9] letter [A-Za-z] id ({letter}|[_])({letter}|{digit}|[_])* %% /*识别规则,每条规则中的动作都用大括号括起来*/ “main”|”int”|”if” {Upper(yytext, yyleng); printf("%s, KEYn", yytext); } {id} {printf("%s, IDn", yytext); } “+”|”-”|”*” {printf("%s, SYMBOLn", yytext); } 1/11/2022 编译原理 Tuesday, January 11, 2022 89
%% /*辅助过程*/ Upper(char *s, int l) { int i; for(i=0; i< l; i++) s[i]=toupper(s[i]); return 1; } void main(void) { yylex(); } 1/11/2022 编译原理 Tuesday, January 11, 2022 90
start 例: LEX源程序, a { } abb { } a bb { } start a 1 2 a b b 4 5 6 a b start b 7 8 3 一. 读LEX源程序,分别生成NFA,用状态图表示为: a 二. 合并成一个NFA: 1 2 ε start 0 ε ε 1/11/2022 编译原理 a 3 4 a 7 b 8 b 5 b 6 b Tuesday, January 11, 2022 99
3. 5. 4 Parser Generator的使用 在VC下运行Parser Generator生成的文件需进行如下设置: 1. VC环境的设置: 具-选择-目录-显示目录为: Include Files中加入D: PARGENINCLUDE Library Files中加入D: PARGEN LIBMSDEV Source Files中加入D: PARGENSOURCE 其中D: PARGEN为Parser Generator的安装路径 2. 对每个Project都需如下设置: 程-设置-C/C++ -预处理程序定义:加入 , YYDEBUG 程-设置-Link –对象/库模块:加入 yld. lib 1/11/2022 编译原理 Tuesday, January 11, 2022 104
• 通过三个全局变量区分所识别的单词: –sym:存放单词的类别, 如beginsym, ident, number –id: 存放用户所定义的标识符的值 –num:存放用户定义的数 char *symbol[32]={"nul", "ident", "number", "plus", "minus", "times", "slash", "oddsym", "eql", "neq", "lss", "leq", "gtr", "geq", "lparen", "rparen", "comma", "semicolon", "period", "becomes", "beginsym", "endsym", "ifsym", "thensym", "whilesym", "writesym", "readsym", "dosym", "callsym", "constsym", "varsym", "procsym"}; 1/11/2022 编译原理 Tuesday, January 11, 2022 107
• 状态图 1/11/2022 编译原理 Tuesday, January 11, 2022 109
PL/0词法分析程序的设计 1/11/2022 编译原理 Tuesday, January 11, 2022 110
1/11/2022 编译原理 Tuesday, January 11, 2022 111
PL/0词法分析程序的设计 1/11/2022 编译原理 Tuesday, January 11, 2022 112
1/11/2022 编译原理 Tuesday, January 11, 2022 113
1/11/2022 编译原理 Tuesday, January 11, 2022 114
1/11/2022 编译原理 Tuesday, January 11, 2022 115
• 状态图 1/11/2022 编译原理 Tuesday, January 11, 2022 116
- Sum0
- Inheritance calculator
- Int max(int x int y)
- Public void drawsquare(int x, int y, int len)
- Public int divide(int a int b)
- Medicis brough
- Int argc char argv
- Int main(int argc, char** argv)
- #include stdio.h int main() int i = 5
- Int main int num 4
- Void main int main
- 7팩토리얼
- Arduino constant int
- Void f(int i) int j=0
- Void swap(int a int b)
- Interface myinterface int foo(int x)
- Max int
- Click to add titleclick to add subtitle
- Click to add subtitle
- Click to add titleclick to add subtitle
- Int cnt
- Ar-lst
- #includestdio.h
- Int main
- 쉽게 풀어 쓴 c언어 express 도전문제
- Include
- Int main
- I^2
- Public static void main int args
- Main() int x y m n
- Void main() int i j charval='j'
- 윤년 순서도
- Int main
- Void main() int i j charval='j'
- #includestdio.h int main()
- #include stdio.h int main()
- Stated and implied main idea examples
- Ways of expressing future time
- Production system characteristics in ai
- Int num;
- Dell bank
- Who.int
- Parallel planes
- Intj 中文
- Int(a)
- Strlen int
- Algebre relationnel
- Int long double
- Int score
- Char int float
- Start num
- Dos int 21
- S jett o sang
- Int scope
- Int num
- The function prototype double mysqrt( int x );
- Piplines
- Nbbd database
- Struct node int data struct node* next
- Array yang sangat banyak elemen nol-nya, dikenal sebagai?
- Throws ioexception
- Global brand database
- Public static int
- Char ch int i scanf c&i
- Int kaç byte
- Int num
- Int ival char cval void *ptr
- Typedef struct tree int info struct *left
- Public void int
- Min int forum
- Int hospital
- What is the alan turing test
- Int pin
- Temario de java
- Declare an array alpha of 15 elements of type int.
- Counter pointer
- Bfs 최단경로
- Public class welcome
- Teflonschale
- Bcom int'l
- Int vs short
- Map(int, input().split())
- Struct node *next
- Int float
- Signed vs unsigned int
- Struct node int data
- Dan int-hout
- Warning
- Double 자료형
- Public int id { get; set; }
- Struct point int x y
- Int val = 0
- Maxint
- Class person string name int age
- Int
- Int num
- Def int
- Class math static int x
- Private int
- Int grade
- Int state
- Tipe data yang terdiri dari aksara
- Int x = 0; for (x=1; x<4; x++); printf("x=%d ", x);
- Int num
- Inurl:num=
- Example of inheritance in java
- 1 char kaç byte
- Int myarray[][] = {{0, 0, 3}, {0, 0, 0}, {7, 0, 0}};
- Unsigned char
- Typedef struct tree
- In the statement int* p, q; p and q are pointer variables.
- Max int c++
- Customer id number (4) not null
- Esa multimedia.esa.int./multimedia/virtual-tour-iss
- Int a=10;
- Who.int
- Int x;