1. 有人知道编译原理实验之词法分析器用C++怎么做吗
#include "globals.h"
#include "util.h"
#include "scan.h"
#include "parse.h"
static TokenType token; /* holds current token */
/* function prototypes for recursive calls */
static TreeNode * stmt_sequence(void);
static TreeNode * statement(void);
static TreeNode * if_stmt(void);
static TreeNode * repeat_stmt(void);
static TreeNode * assign_stmt(void);
static TreeNode * read_stmt(void);
static TreeNode * write_stmt(void);
static TreeNode * exp(void);
static TreeNode * simple_exp(void);
static TreeNode * term(void);
static TreeNode * factor(void);
static void syntaxError(char * message)
{ fprintf(listing,"\n>>> ");
fprintf(listing,"Syntax error at line %d: %s",lineno,message);
Error = TRUE;
}
static void match(TokenType expected)
{ if (token == expected) token = getToken();
else {
syntaxError("unexpected token -> ");
printToken(token,tokenString);
fprintf(listing," ");
}
}
TreeNode * stmt_sequence(void)
{ TreeNode * t = statement();
TreeNode * p = t;
while ((token!=ENDFILE) && (token!=END) &&
(token!=ELSE) && (token!=UNTIL))
{ TreeNode * q;
match(SEMI);
q = statement();
if (q!=NULL) {
if (t==NULL) t = p = q;
else /* now p cannot be NULL either */
{ p->sibling = q;
p = q;
}
}
}
return t;
}
TreeNode * statement(void)
{ TreeNode * t = NULL;
switch (token) {
case IF : t = if_stmt(); break;
case REPEAT : t = repeat_stmt(); break;
case ID : t = assign_stmt(); break;
case READ : t = read_stmt(); break;
case WRITE : t = write_stmt(); break;
default : syntaxError("unexpected token -> ");
printToken(token,tokenString);
token = getToken();
break;
} /* end case */
return t;
}
TreeNode * if_stmt(void)
{ TreeNode * t = newStmtNode(IfK);
match(IF);
if (t!=NULL) t->child[0] = exp();
match(THEN);
if (t!=NULL) t->child[1] = stmt_sequence();
if (token==ELSE) {
match(ELSE);
if (t!=NULL) t->child[2] = stmt_sequence();
}
match(END);
return t;
}
TreeNode * repeat_stmt(void)
{ TreeNode * t = newStmtNode(RepeatK);
match(REPEAT);
if (t!=NULL) t->child[0] = stmt_sequence();
match(UNTIL);
if (t!=NULL) t->child[1] = exp();
return t;
}
TreeNode * assign_stmt(void)
{ TreeNode * t = newStmtNode(AssignK);
if ((t!=NULL) && (token==ID))
t->attr.name = String(tokenString);
match(ID);
match(ASSIGN);
if (t!=NULL) t->child[0] = exp();
return t;
}
TreeNode * read_stmt(void)
{ TreeNode * t = newStmtNode(ReadK);
match(READ);
if ((t!=NULL) && (token==ID))
t->attr.name = String(tokenString);
match(ID);
return t;
}
TreeNode * write_stmt(void)
{ TreeNode * t = newStmtNode(WriteK);
match(WRITE);
if (t!=NULL) t->child[0] = exp();
return t;
}
TreeNode * exp(void)
{ TreeNode * t = simple_exp();
if ((token==LT)||(token==EQ)) {
TreeNode * p = newExpNode(OpK);
if (p!=NULL) {
p->child[0] = t;
p->attr.op = token;
t = p;
}
match(token);
if (t!=NULL)
t->child[1] = simple_exp();
}
return t;
}
TreeNode * simple_exp(void)
{ TreeNode * t = term();
while ((token==PLUS)||(token==MINUS))
{ TreeNode * p = newExpNode(OpK);
if (p!=NULL) {
p->child[0] = t;
p->attr.op = token;
t = p;
match(token);
t->child[1] = term();
}
}
return t;
}
TreeNode * term(void)
{ TreeNode * t = factor();
while ((token==TIMES)||(token==OVER))
{ TreeNode * p = newExpNode(OpK);
if (p!=NULL) {
p->child[0] = t;
p->attr.op = token;
t = p;
match(token);
p->child[1] = factor();
}
}
return t;
}
TreeNode * factor(void)
{ TreeNode * t = NULL;
switch (token) {
case NUM :
t = newExpNode(ConstK);
if ((t!=NULL) && (token==NUM))
t->attr.val = atoi(tokenString);
match(NUM);
break;
case ID :
t = newExpNode(IdK);
if ((t!=NULL) && (token==ID))
t->attr.name = String(tokenString);
match(ID);
break;
case LPAREN :
match(LPAREN);
t = exp();
match(RPAREN);
break;
default:
syntaxError("unexpected token -> ");
printToken(token,tokenString);
token = getToken();
break;
}
return t;
}
/****************************************/
/* the primary function of the parser */
/****************************************/
/* Function parse returns the newly
* constructed syntax tree
*/
TreeNode * parse(void)
{ TreeNode * t;
token = getToken();
t = stmt_sequence();
if (token!=ENDFILE)
syntaxError("Code ends before file\n");
return t;
}
上面是一个语法分析器的主代码部分它可以识别类似下面的代码,但是由于篇幅有限,上面的代码不是完整代码,完整代码太长,还有好几个文件。
read x; { input an integer }
if 0 < x then { don't compute if x <= 0 }
fact := 1;
repeat
fact := fact * x;
x := x - 1
until x = 0;
write fact { output factorial of x }
end
2. C++:算术表达式求值
1/(1+1/(1+1/(x+y)));
x*(x*(x*(a*x+b)+c)+d)+e;
log(1+pow(fabs((a+b)/(a-b)),10));
sqrt(1+pi/2*cos(48));
1/tan((1-x*x)/(1+x*x));
//由于c语言中没提供cot函数,所以就用tan的倒数表示了。
log10(a*a+a*b+b*b);
3. 编译原理考试问题:已知表达式文法G(Exp)
简单起见,用E代表Exp,用T代表Term,用F代表Factor。下面是所求属性文法
(1)E→ E1 + T E.val:=E1.val+T.val /* 为了区别→两侧的E, →右侧的E用E1表示 */
(2)E→
T E.val:=T.val
(3)T→ T1 * F T.val:=T1.val*F.val
(4)T→
F T.val:=F.val
(5)F→(E) F.val:=E.val
(6)F→num F.val:=num.val
4. 正则表达式
正则经常用于js 判断手机号,邮箱等,通过简单的办法来实现强大的功能
符号解释
字符 描述
\ 将下一个字符标记为一个特殊字符、或一个原义字符、或一个 向后引用、或一个八进制转义符。例如,'n' 匹配字符 "n"。'\n' 匹配一个换行符。序列 '\\' 匹配 "\" 而 "\(" 则匹配 "("。
^ 匹配输入字符串的开始位置。如果设置了 RegExp 对象的 Multiline 属性,^ 也匹配 '\n' 或 '\r' 之后的位置。
$ 匹配输入字符串的结束位置。如果设置了RegExp 对象的 Multiline 属性,$ 也匹配 '\n' 或 '\r' 之前的位置。
* 匹配前面的子表达式零次或多次。例如,zo* 能匹配 "z" 以及 "zoo"。* 等价于{0,}。
+ 匹配前面的子表达式一次或多次。例如,'zo+' 能匹配 "zo" 以及 "zoo",但不能匹配 "z"。+ 等价于 {1,}。
? 匹配前面的子表达式零次或一次。例如,"do(es)?" 可以匹配 "do" 或 "does" 中的"do" 。? 等价于 {0,1}。
{n} n 是一个非负整数。匹配确定的 n 次。例如,'o{2}' 不能匹配 "Bob" 中的 'o',但是能匹配 "food" 中的两个 o。
{n,} n 是一个非负整数。至少匹配n 次。例如,'o{2,}' 不能匹配 "Bob" 中的 'o',但能匹配 "foooood" 中的所有 o。'o{1,}' 等价于 'o+'。'o{0,}' 则等价于 'o*'。
{n,m} m 和 n 均为非负整数,其中n <= m。最少匹配 n 次且最多匹配 m 次。例如,"o{1,3}" 将匹配 "fooooood" 中的前三个 o。'o{0,1}' 等价于 'o?'。请注意在逗号和两个数之间不能有空格。
? 当该字符紧跟在任何一个其他限制符 (*, +, ?, {n}, {n,}, {n,m}) 后面时,匹配模式是非贪婪的。非贪婪模式尽可能少的匹配所搜索的字符串,而默认的贪婪模式则尽可能多的匹配所搜索的字符串。例如,对于字符串 "oooo",'o+?' 将匹配单个 "o",而 'o+' 将匹配所有 'o'。
. 匹配除 "\n" 之外的任何单个字符。要匹配包括 '\n' 在内的任何字符,请使用象 '[.\n]' 的模式。
x|y 匹配 x 或 y。例如,'z|food' 能匹配 "z" 或 "food"。'(z|f)ood' 则匹配 "zood" 或 "food"。
[xyz] 字符集合。匹配所包含的任意一个字符。例如, '[abc]' 可以匹配 "plain" 中的 'a'。
[^xyz] 负值字符集合。匹配未包含的任意字符。例如, '[^abc]' 可以匹配 "plain" 中的'p'。
[a-z] 字符范围。匹配指定范围内的任意字符。例如,'[a-z]' 可以匹配 'a' 到 'z' 范围内的任意小写字母字符。
[^a-z] 负值字符范围。匹配任何不在指定范围内的任意字符。例如,'[^a-z]' 可以匹配任何不在 'a' 到 'z' 范围内的任意字符。
\d 匹配一个数字字符。等价于 [0-9]。
\D 匹配一个非数字字符。等价于 [^0-9]。
\f 匹配一个换页符。等价于 \x0c 和 \cL。
\n 匹配一个换行符。等价于 \x0a 和 \cJ。
\r 匹配一个回车符。等价于 \x0d 和 \cM。
\s 匹配任何空白字符,包括空格、制表符、换页符等等。等价于 [ \f\n\r\t\v]。
\S 匹配任何非空白字符。等价于 [^ \f\n\r\t\v]。
\t 匹配一个制表符。等价于 \x09 和 \cI。
\v 匹配一个垂直制表符。等价于 \x0b 和 \cK。
\w 匹配包括下划线的任何单词字符。等价于'[A-Za-z0-9_]'。
\W 匹配任何非单词字符。等价于 '[^A-Za-z0-9_]'。
具体怎么使用还得多看例子,对照解释
5. 这个用C语言写的计算器的思路是什么
对初学编程者来说,这个程序的原理确实难了点,因为它用到了编译原理的知识.
即如果设一个四则运算表达式的形式为S,那么它一定是一个以等号结尾的运算式,即S->exp=,->是推导符号.
运算式exp又可以继续推导成
exp->exp+term|exp-term|term
exp表示加减运算,term表示乘除运算.这个推导式反映了乘除的优先级比加减高.
即要先计算乘除式的结果,再来加减.
term可以推导如下:
term->term*factor|term/factor|factor
factor->num|(E)
factor是数字或者一个被括号括住的运算式,表示最高优先级.
数字本身是不带运算的,是原子性的,肯定是最高优先级.
括号是被规定了优先计算.
这个程序的代码就是按照上面的推导式,用递归方式来分析运算式的.
6. 计算器的实现(C语言)
这个计算器是卖不出去的,因为功能过于简单o(∩_∩)o...
不支持多目运算,进制转换只能将十进制整数转换为其他进制整数,还有很多地方可以改进的,由于时间关系就没一一写出来了。
#include<stdio.h>
void add(void);
void sub(void);
void mul(void);
void div(void);
void sc(void);
void er(void);
int main()
{
int ch;
while(1)
{
printf("\n[Caculator]\n1.+ 2.- 3.* 4./ 5.Size Change 6.Exchange Rate 7.Quit\n");
do{
printf("Input Your Chooice:");
scanf("%d", &ch);
}while(!(ch > 0 && ch < 8));
if(ch == 7)
break;
switch(ch)
{
case 1:
add();
break;
case 2:
sub();
break;
case 3:
mul();
break;
case 4:
div();
break;
case 5:
sc();
break;
case 6:
er();
break;
default:
break;
}
}
return 0;
}
void add()
{
float a,b;
printf("[add]:Input a and b:");
scanf("%f%f",&a,&b);
printf("[add]Answer:%f\n",a+b);
}
void sub()
{
float a,b;
printf("[sub]:Input a and b:");
scanf("%f%f",&a,&b);
printf("[sub]Answer:%f\n",a-b);
}
void mul()
{
float a,b;
printf("[mul]:Input a and b:");
scanf("%f%f",&a,&b);
printf("[mul]Answer:%f\n",a*b);
}
void div()
{
float a,b;
printf("[div]:Input a and b:");
scanf("%f%f",&a,&b);
printf("[div]Answer:%f\n",a/b);
}
void sc()
{
int a,b,i;
char c[100];
printf("[Size Change]Input the number and change size:");
scanf("%d%d",&a,&b);
for(i = 0;a/b != 0; i++)
{
c[i] = a%b;
a /= b;
}
c[i] = a%b;
printf("[Size Change]:");
for(;i >= 0;i--)
{
printf("%d\n", c[i]);
}
}
void er()
{
float a,b;
printf("[Exchange Rate]:Input the money and rate:");
scanf("%f%f",&a,&b);
printf("[Exchange Rate]:%f\n", a*b);
}
7. 求一个用C++编过计算器的,就是那种加减乘除三角函数可以写一排算的
//*************************************
//数学表达式解析类
//*************************************
//Expression_Parser.cpp
#include<stdio.h>
#include<string.h>
#include<math.h>
constdoublePI=3.141592654;
//将角度转换成弧度
doubledegTorad(doubledeg)
{
return(2*PI*deg)/360;
}
//将中缀表达式转换为后缀表达式(逆波兰式)
voidtrans(chara[],charb[])
{
charstock[128]={0};
inttop=0;
intlen=0;
inti=0;
intj=0;
top=-1;
j=-1;
len=strlen(a);
for(i=0;i<len;i++)
{
switch(a[i])
{
case'(':
stock[++top]='(';
break;
case'+':
case'-':
while(top>=0&&stock[top]!='(')
{
b[++j]=stock[top--];
}
stock[++top]='';
stock[++top]=a[i];
break;
case'*':
case'/':
while(top>=0&&stock[top]!='('&&stock[top]!='+'&&stock[top]!='-')
{
b[++j]=stock[top--];
}
stock[++top]='';
stock[++top]=a[i];
break;
case's':
case'c':
case't':
while(top>=0&&stock[top]!='('&&stock[top]!='+'&&stock[top]!='-'&&stock[top]!='*'&&stock[top]!='/')
{
b[++j]=stock[top--];
}
stock[++top]='';
stock[++top]=a[i];
break;
case'v':
case'^':
while(top>=0&&stock[top]!='('&&stock[top]!='+'&&stock[top]!='-'&&stock[top]!='*'&&stock[top]!='/'&&stock[top]!='s'&&stock[top]!='c'&&stock[top]!='t')
{
b[++j]=stock[top--];
}
stock[++top]='';
stock[++top]=a[i];
break;
case'L':
while(top>=0&&stock[top]!='('&&stock[top]!='+'&&stock[top]!='-'&&stock[top]!='*'&&stock[top]!='/'&&stock[top]!='s'&&stock[top]!='c'&&stock[top]!='t'&&stock[top]!='v'&&stock[top]!='^')
{
b[++j]=stock[top--];
}
stock[++top]='';
stock[++top]=a[i];
break;
case')':
while(stock[top]!='(')
{
b[++j]=stock[top--];
}
top--;
break;
default:
b[++j]=a[i];
if(i==len-1||a[i+1]<'0'||a[i+1]>'9')
{
if(a[i+1]!='.')
{
b[++j]='';
}
}
break;
}
}
while(top>=0)
{
b[++j]=stock[top--];
}
b[++j]='