三九宝宝网宝宝教育教学论文

用c语言编写一个词法分析器用来识别:由任意个a或b开始后接aa再自

03月14日 编辑 39baobao.com

[c语言编程:有3个整数a b c由键盘输入请输出其中最大的数]【例】输入三个整数,输出最大数和最小数。 main(){ int a,b,c,max,min; printf("input three numbers: "); scanf("%d%d%d",&a,&b,&c); if(a>b) {max=a;min=b;} else {max=b;min=...+阅读

我们通过这个正规式可以知道,最后4个字符是确定的,即aa+1或aa-1。然后再判断前面的字符是否为a或b。我的想法是从后往前判断,只有当前的条件符合时再进行下面的判断。

我们可以将正规式分为4部分,于是得到一下几个判断的原则:

1.读入的字符串长度必须大于等于4;

2.最后一个字符必须为'1';

3.倒数第2个字符必须为‘+’或‘-’;

4.倒数第3个和第4个字符必须为'a';

5.前面的字符必须为'a'或'b';

下面是具体的程序:

#include

#include

#include

//判断输入的字符串是否符合正规式

//返回0表示不符合,返回1表示符合正规式

int func(char *s)

{

int i, j, t;

char c;

t = strlen(s);

//如果输入的长度小于4,一定不符合正规式

if(t<4)

{

return 0;

}

//如果最后一个不是1,则不符合正规式

if(s[t-1]!='1')

{

return 0;

}

//如果最后是1但倒数第二个不是+或-,则不符合正规式

if(s[t-2]!='+' & s[t-2]!='-')

{

return 0;

}

//如果前两个条件符合,则检测是否存在连续的两个a

//只要其中一个不为a,则不符合正规式

if(s[t-4]!='a' || s[t-3]!='a')

{

return 0;

}

//如果前面的条件符合,则进行此判断

//此时不用判断读入的字符串长度是否大于4或者等于4,

//因为如果字符串的长度等于4时,根本不进入循环

for(i=0; i

{

if(s[i]!='a' & s[i]!='b')

{

return 0;

}

}

//如果条件都符合,则符合正规式

return 1;

}

int main()

{

char s[200];

while(scanf("%s", s)==1)

{

if(func(s)==1)

{

printf("yes\n");

}

else

{

printf("no\n");

}

}

return 0;

}

/*

测试用例:

aa+1 yes

aa-1 yes

aaa+1 yes

aaaaa+1 yes

bbbbaa-1 yes

abababaa+1 yes

aaabbaa-1 yes

a+1 no

aa+2 no

aa*1 no

aaab+1 no

abababcaa-1 no

aaabbbcccaa+1 no

*/

急求用c编写一个简单的词法分析器!!

#include "globals.h"#include "util.h"#include "scan.h" typedef enum { START, INID, INNUM, INCOMMENT, INCOMMENT_ASTERISK, INDIV, INLT, INGT, INASSIGN, INNOT, DONE } StateType; char token_string[MAXTOKENLEN+1];#define BUFLEN 256 static char linebuf[BUFLEN]; static int linepos = 0; static int bufsize = 0; static int eof = 0; static char get_next_char() { if(eof) return EOF; if(linepos return linebuf[linepos++]; else { lineno++; if(fgets(linebuf, BUFLEN-1, source)) { if(echo_source) fprintf(listing, "%4d: %s", lineno, linebuf); bufsize = strlen(linebuf); linepos = 0; return linebuf[linepos++]; } else { eof = 1; return EOF; } } } static void unget_char() { if(!eof) linepos--; } static struct { char *str; TokenType tok; } reserved_words[MAXRESERVED] = { {"if", IF}, {"else", ELSE}, {"int", INT}, {"return", RETURN}, {"void", VOID}, {"while", WHILE} }; static TokenType reserved_lokkup(const char *s) { int i; for ( i = 0; i if(!strcmp(reserved_words[i].str, s)) return reserved_words[i].tok; } return ID; } TokenType get_token() { int index = 0; TokenType cur_token; StateType state = START; int save; while(state != DONE) { char c = get_next_char(); save = TRUE; switch(state) { case START: if(isdigit(c)) state = INNUM; else if(isalpha(c)) state = INID; else if(c == '/') state = INDIV; else if(c == ' state = INLT; else if(c == '>') state = INGT; else if(c == '=') state = INASSIGN; else if(c == '!') state = INNOT; else if(isblank(c) || c == '\n') save = FALSE; else { state = DONE; switch(c) { case EOF: save = FALSE; cur_token = ENDFILE; break; case '+': cur_token = PLUS; break; case '-': cur_token = MINUS; break; case '*': cur_token = MUL; if (get_next_char() == '/') { cur_token = ERROR; } else unget_char(); break; case ';': cur_token = SEMI; break; case ',': cur_token = COMMA; break; case '(': cur_token = LPAREN; break; case ')': cur_token = RPAREN; break; case '[': cur_token = LBRACKET; break; case ']': cur_token = RBRACKET; break; case '{': cur_token = LBRACE; break; case '}': cur_token = RBRACE; break; default: cur_token = ERROR; break; } } break; case INCOMMENT: save = FALSE; if(c == '*') state = INCOMMENT_ASTERISK; else if(c == EOF) { cur_token = ERROR; state = DONE; } break; case INCOMMENT_ASTERISK: save = FALSE; if(c == '/') { state = START; index = 0; }else if(c == '*'){ }else if(c == EOF) { cur_token = ERROR; state = DONE; }else{ state = INCOMMENT; } break; case INID: if(!isalpha(c)) { unget_char(); state = DONE; save = FALSE; cur_token = ID; } break; case INNUM: if(!isdigit(c)) { unget_char(); state = DONE; save = FALSE; cur_token = NUM; } break; case INDIV: if(c == '*') { state = INCOMMENT; save = FALSE; } else { unget_char(); save = FALSE; state = DONE; cur_token = DIV; } break; case INLT: if(c == '=') { state = DONE; cur_token = LE; } else { unget_char(); state = DONE; save = FALSE; cur_token = LT; } break; case INGT: if(c == '=') { state = DONE; cur_token = GE; } else { unget_char(); save = FALSE; state = DONE; cur_token = GT; } break; case INASSIGN: if(c == '=') { state = DONE; cur_token = EQ; } else { unget_char(); save = FALSE; state = DONE; cur_token = ASSIGN; } break; case INNOT: state = DONE; if(c == '=') { cur_token = NEQ; } else { unget_char(); save = FALSE; cur_token = ERROR; } break; case DONE: default: fprintf(listing, "Scanner Bug: state = %d\n", state); state = DONE; cur_token = ERROR; break; } if(save & (index token_string[index++] = c; if(state == DONE) { token_string[index] = '\0'; if(cur_token == ID) cur_token = reserved_lokkup(token_string); } } if(trace_scan) { fprintf(listing, "\t%d: ", lineno); print_token(cur_token, token_string); } return cur_token; }

求一份C语言编写的极其简化的C词法分析器高手应该不超过20分钟

#include #include using namespace std; #define MAX 22 char ch =' '; string key[15]={"begin","end","if","then","else","while","write","read", "do", "call","const","char","until","procedure","repeat"}; int Iskey(string c){ //关键字判断 int i; for(i=0;i='a'))||((c<='Z')&&(c>='A'))) return 1; else return 0; } int IsDigit(char c){ //判断是否为数字 if(c>='0'&&c<='9') return 1; else return 0; } void analyse(FILE *fpin){ string arr=""; while((ch=fgetc(fpin))!=EOF) { arr=""; if(ch==' '||ch=='\t'||ch=='\n'){} else if(IsLetter(ch)){ while(IsLetter(ch)||IsDigit(ch)) { if((ch<='Z')&&(ch>='A')) ch=ch+32; arr=arr+ch; ch=fgetc(fpin); } fseek(fpin,-1L,SEEK_CUR); if (Iskey(arr)){cout<' :{ch=fgetc(fpin); if(ch=='=') cout<<">="<<"\t$运算符"<')cout<<">>"<<"\t$输入控制符"<"<<"\t$运算符"<') cout<<"<>"<<"\t$运算符"<>in_fn; if((fpin=fopen(in_fn,"r"))!=NULL) break; else cout<<;"文件路径错误!请输入源文件名(包括路径和后缀名):"; } cout<<"\n********************分析如下*********************"<

以下为关联文档:

用C语言编写一个计算薪水的程序/*工资计算程序*/ #include main() { float originWage; /*应发工资*/ float realWage; /*实发工资*/ float tax; /*所缴税款*/ int i,hour,amount,money; printf("请选择工资种...

怎么用C语言编写100以内任意整数的阶乘啊int n; int r[600]={0}; int count(n) { int i; for(i=0;i<600;i++) r[i]*=n; for(i=599;i>0;i--) { r[i-1]=r[i-1]+r[i]/10; r[i]=r[i]%10; } } int main() { int i; int r...

用C语言编写的代码程序 B A可立即执行 B是一个源程序 CC语言写的代码程序肯定是源程序,B是对的,它不能立即执行,必须经过编译成可执行代码, 所以A是错误的; 如果这个源程序中不含有main函数,编译后的代码也是不可执行的,所以C是错误的; C...

用C语言编写一个图书管理系统#include#includestruct sale/*商品名的定义*/{int number; char name[20]; float price; long count; long time; long date;}; /*各个函数*/ void input(struct sale s[7]);/...

c语言问题计算请编写函数isprime用来判断一个整数a是否为#include "stdio.h" int isprime(int a) { int j; for(j=2;j<a;j++) if(a%j==0) return 0; else return 1; } void main() { int x,y; printf("请输入一个整数:"); scanf("%d",&x); y=...

用c语言编写一个简单的程序main() { int a,b,i; bool flag = true; for (i = 0; i < 6; i++) { scanf("%d", &a); if (flag) { b = a; flag = false; } else { if (b > a) b = a; } } prinf("%d", b); }...

C语言用递归函数打印 a a b a b c a b c d a b c d e a b c d e f a b c d e#include #include void print(int num) { int i; if (num == 1) { printf("a "); return; } print(num - 1); for (i = 0; i printf("%c ", 'a' + i); } void main() { print(6...

高手指点:用C语言编写一个读写#ifndef BMPRW_H #define BMPRW_H typedef struct { // 长度12字节 // short bftype; // 固定为"BM",VC不能读取一个short,故省略 int bfSize; // 位图文件的大小,以字节为单位 sh...

用C语言编写一个程序用C语言编写一个程序,C语言编写程序:#include <stdio.h> main() { char *month_name[13]={"illegal month","January","February","March","April", "May","June","July","August","September","Oct...

推荐阅读
图文推荐