请问词法分析阶段常数用不用去识别负数是不是常数?...

词法分析及词法分析程序_百度文库
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
文档贡献者
评价文档:
词法分析及词法分析程序
大小:1.18MB
登录百度文库,专享文档复制特权,财富值每天免费拿!
你可能喜欢03第3章 词法分析01_百度文库
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
文档贡献者
评价文档:
03第3章 词法分析01
大小:225.00KB
登录百度文库,专享文档复制特权,财富值每天免费拿!
你可能喜欢编译原理,词法分析,实验报告
通过设计、开发一个高级语言的词法分析程序,加深对课堂教学内容(包括正规文法、正规表达式、有限自动机、NFA到DFA的转换、DFA的最小化)的理解,提高词法分析方法的实践能力。
(1)深入理解、掌握有限自动机及其应用;
(2)掌握根据语言的词法规则构造识别其单词的有限自动机的方法;
(3)掌握NFA到DFA的等价变换方法、DFA最小化的方法;
(4)掌握设计、编码、调试词法分析程序的技术与方法,具体实现S语言的词法分析程序。
&& begin& if& then& while& do& end&
:& : =& +& -& *& /&
&=& =& ;& (& )& #
ID = letter (letter |
NUM = digit
syn,tokensum
begin x:=9: if x&9 then x:=2*x+1/3; end
(1,begin)(10,x)(18,:=)(11,9)(26,;)(2,if)
Char *rwtab[6] =
{“begin”, “if”, “then”, “while”, “do”, “end”,};
2syn,tokensum
3tokensumsyn3-2
&stdlib.h&
&string.h&
prog[100],ch,token[8];
p=0,syn,n,i;
*keyword[6]={"begin","then","if","while","do","end"};
Irparse();
statement();
expression_r();
int select=-1;
printf("please input sentence, end of '#' !\n");
&&&&&&&&&&&&&
ch=getchar();
&&&&&&&&&&&&&
prog[p++]=
}while(ch!='#');
2 \n& 1.\n& 2.\n");
&&&&&&&&&&&&&
scanf("%d",&select);
if(select==1)
&&&&&&&&&&&&&
&&&&&&&&&&&&&
&&&&&&&&&&&&&
&&&&&&&&&&&&&
switch(syn)
&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&
case -1:printf("
&&&&&&&&&&&&&&&&&&&&
:printf("&%d,%s&\n",syn,token);
&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&
}while(syn!=0);
&&&&&&&&&&&&&
else if(select==2)&&&
&&&&&&&&&&&&&
if(syn==1)
&&&&&&&&&&&&&
{Irparse();}//begin
&&&&&&&&&&&&&
{printf("! begin\n");}
if(syn==6)//end
&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&
if(syn==0)
&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&
{printf("! '#'\n");}
&&&&&&&&&&&&&
else{printf("! 'end'\n");}
getchar();
&&&&&&&&&&&&&
for(n=0;n&8;n++)
{token[n]='\0';}
ch=prog[p++];
&&&&&&&&&&&&&
while(ch==' '){ch=prog[p++];}
&&&&&&&&&&&&&
&&&&&&&&&&&&&
if((ch&='a'&&ch&='z')||(ch&='A'&&ch&='Z'))
&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&&&&&&&&
token[n++]=
&&&&&&&&&&&&&&&&&&&&&&&&&&&
ch=prog[p++];
&&&&&&&&&&
}while((ch&='a'&&ch&='z')||(ch&='a'&&ch&='z')||(ch&='0'&&ch&='9'));
&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&
for(n=0;n&6;n++)
&&&&&&&&&&&&&&&&&&&&
{if(strcmp(token,keyword[n])==0)
&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&
&&&&&&&&&&&&&
if(ch&='0'&&ch&='9')
&&&&&&&&&&&&&
&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&&&&&&&&
token[n++]=prog[p++];
&&&&&&&&&&&&&&&&&&&&&&&&&&&
ch=prog[p];
&&&&&&&&&&&&&&&&&&&&
}while(ch&='0'&&ch&='9');
&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&
&&&&&&&&&&&&&
&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&
//ch=prog[p++];
&&&&&&&&&&&&&&&&&&&&
switch(ch)
&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&
case '+':syn=13;token[0]=
&&&&&&&&&&&&&&&&&&&&
case '-':syn=14;token[0]=
&&&&&&&&&&&&&&&&&&&&
case '*':syn=15;token[0]=
&&&&&&&&&&&&&&&&&&&&
case '/':syn=16;token[0]=
&&&&&&&&&&&&&&&&&&&&
case ':':syn=17;token[0]=
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
ch=prog[p++];
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
if(ch=='='){token[1]=syn++;}
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&
case '&':syn=20;token[0]=
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
ch=prog[p++];
&&&&&&&&&&&&&&&&&&&&&&&&&&&
if(ch=='&'){token[1]=syn++;}
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
else if(ch=='='){token[1]=syn=syn+2;}
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&
case '&':syn=23;token[0]=
&&&&&&&&&&&&&&&&&&&&&&&&&&&
ch=prog[p++];
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
if(ch=='='){token[1]=syn++;}
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&
case '=':syn=25;token[0]=
&&&&&&&&&&&&&&&&&&&&
case ';':syn=26;token[0]=
&&&&&&&&&&&&&&&&&&&&
case '(':syn=27;token[0]=
&&&&&&&&&&&&&&&&&&&&
case ')':syn=28;token[0]=
&&&&&&&&&&&&&&&&&&&&
case '#':syn=0;token[0]=
&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&
default: printf("! \n");syn=-1;
&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&
&&&&&&&&&&&&&
statement();
while(syn==26)//;
&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&
statement();
&&&&&&&&&&&&&
statement()
if(syn==10)
&&&&&&&&&&&&&
&&&&&&&&&&&&&
if(syn==18)
&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&
expression_r();
&&&&&&&&&&&&&
&&&&&&&&&&&&&
&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&
printf("! \n");
&&&&&&&&&&&&&
&&&&&&&&&&&&&
printf("!& \n");
expression_r()
while(syn==13||syn==14)//+ -
&&&&&&&&&&&&&
&&&&&&&&&&&&&
while(syn==15||syn==16)//* /
&&&&&&&&&&&&&
&&&&&&&&&&&&&
if(syn==10||syn==11)
&&&&&&&&&&&&&
else if(syn==27)
&&&&&&&&&&&&&
&&&&&&&&&&&&&
expression_r();
&&&&&&&&&&&&&
if(syn==28)
&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&
&&&&&&&&&&&&&
else {printf("! ')'\n");}
else {printf("! \n");}
2begin x=4;if
x&0 then x+3*x+1/2; end #
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。1731人阅读
&词法分析阶段是编译过程的第一个阶段。这个阶段的任务是从左到右一个字符一个字符地读入源程序,即对构成源程序的字符流进行扫描然后根据构词规则识别单词(也称单词符号或符号,如标识符、常数、运算符、定界符等)。词法分析程序实现这个任务。词法分析程序可以使用Lex等工具自动生成。
词法分析是从左到右逐个字符对构成源程序的字符串进行扫描,依据词法规则,识别出一个一个的单词(token),把作为字符串的源程序变为等价的单词串序列。执行词法分析的程序称为词法分析器,也称为扫描器(scanner)。源程序中的单词符号经扫描器分析,一般产生二元式:单词种别和单词自身的值。单词种别通常用整数编码,如果一个种别只含一个单词符号,那么对这个单词符号,种别编码就完全代表它自身的值了。若一个种别含有许多个单词符号,那么,对于它的每个单词符号,除了给出种别编码以外,还应给出自身的值。
In computer science, lexical analysis is the process of converting a sequence of characters into a sequence of tokens. Programs performing lexical analysis are called lexical analyzers or lexers. A lexer is often organized as separate scanner and tokenizer functions, though the boundaries may not be clearly defined.
The first stage, the scanner, is usually based on a finite state machine. It has encoded within it information on the possible sequences of characters that can be contained within any of the tokens it handles (individual instances of these character sequences are known as lexemes). For instance, an integer token may contain any sequence of numerical digit characters. In many cases, the first non-whitespace character can be used to deduce the kind of token that follows and subsequent input characters are then processed one at a time until reaching a character that is not in the set of characters acceptable for that token (this is known as the maximal munch rule). In some languages the lexeme creation rules are more complicated and may involve backtracking over previously read characters.
在计算机科学和语言学中,语法分析(parsing)是根据某种给定的形式文法(formal grammar)对输入的单词(token)序列进行分析并确定其语法结构的一种过程。而语法分析器通常是以编译器或解释器的组件出现的,它的作用是从输入中分析出其结构并将其转换为在后续处理过程中更易于访问的数据结构(一般是树类的数据结构),并检测可能存在的语法错误。语法分析器通常使用一个词法分析器(lexer)从输入的字符流中分离出一个个的&单词&,并将单词流作为其输入。在实际开发中,语法分析器可以手工编写,也可以使用自动生成程序(如yacc之类)根据一个使用巴科斯范式描述的形式文法来生成其高级语言代码。词法分析器阶段的任务:从左至右逐个读入源程序,对源程序的字符流进行扫描和分析,识别出是否为该类别程序语言的保留字,其他的单词则标为用户定义的标识符。另外,在词法分析阶段,可以分析程序的用户自定义的标识符是否符合构词规则。并表标识出行号位置。
版权声明:本文为博主原创文章,未经博主允许不得转载。
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场编译原理-词法分析_百度文库
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
文档贡献者
评价文档:
编译原理-词法分析
大小:721.00KB
登录百度文库,专享文档复制特权,财富值每天免费拿!
你可能喜欢

我要回帖

更多关于 叙词法 的文章

 

随机推荐