- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是我的青年
%{
#include <stdio.h>
#include <ctype.h>
#include "lex.yy.c"
void yyerror (s) /* Called by yyparse on error */
char *s;
{
printf ("%s\n", s);
}
%}
%start program
%union{
int value;
char * string;
}
%token <value> NUM INT VOID WHILE IF THEN ELSE READ WRITE RETURN LE GE EQ NE
%token <string> ID
%token <value> INTEGER
%left '|'
%left '&'
%left '+' '-'
%left '*' '/' '%'
%left UMINUS
%%
program : decllist
{fprintf(stderr, "program");}
;
decllist : dec
{fprintf(stderr, "\n dec");}
| dec decllist
;
dec : vardec
{fprintf(stderr, "vardec");}
| fundec
{fprintf(stderr, "YEAH");}
;
typespec : INT
| VOID
;
vardec : typespec ID ';'
{fprintf(stderr, "yep");}
| typespec ID '[' NUM ']' ';'
{fprintf(stderr, "again");}
;
fundec : typespec ID '(' params ')' compoundstmt
;
params : VOID
| paramlist
;
paramlist : param
| param ',' paramlist
;
param : typespec ID
| typespec ID '['']'
;
compoundstmt : '{' localdeclerations statementlist '}'
;
localdeclerations :/* empty */
|vardec localdeclerations
;
statementlist : /* empty */
| statement statementlist
;
statement : expressionstmt
| compoundstmt
| selectionstmt
| iterationstmt
| assignmentstmt
| returnstmt
| readstmt
| writestmt
;
expressionstmt : expression ';'
| ';'
;
assignmentstmt : var '=' expressionstmt
;
selectionstmt : IF '(' expression ')' statement
| IF '(' expression ')' statement ELSE statement
;
iterationstmt : WHILE '(' expression ')' statement
;
returnstmt : RETURN ';'
| RETURN expression ';'
;
writestmt : WRITE expression ';'
;
readstmt : READ var ';'
;
expression : simpleexpression
;
var : ID
| ID '[' expression ']'
;
simpleexpression : additiveexpression
| additiveexpression relop simpleexpression
;
relop : LE
| '<'
| '>'
| GE
| EQ
| NE
;
additiveexpression : term
| term addop term
;
addop : '+'
| '-'
;
term : factor
| term multop factor
;
multop : '*'
| '/'
;
factor : '(' expression ')'
| NUM
| var
| call
;
call : ID '(' args ')'
;
args : arglist
| /* empty */
;
arglist : expression
| expression ',' arglist
;
%%
main(){
yyparse();
}
%{
int mydebug=1;
int lineno=0;
#include "y.tab.h"
%}
%%
int {if (mydebug) fprintf(stderr, "int found\n");
return(INT);
}
num {if (mydebug) fprintf(stderr, "num found\n");
return(NUM);
}
void {if (mydebug) fprintf(stderr, "void found \n");
return(VOID);
}
while {if (mydebug) fprintf(stderr, "while found \n");
return(WHILE);
}
if {if (mydebug) fprintf(stderr, "if found \n");
return(IF);
}
then {if (mydebug) fprintf(stderr, "then found \n");
return(THEN);
}
else {if (mydebug) fprintf(stderr, "else found \n");
return(ELSE);
}
read {if (mydebug) fprintf(stderr, "read found \n");
return(READ);
}
write {if (mydebug) fprintf(stderr, "void found \n");
return(WRITE);
}
return {if (mydebug) fprintf(stderr, "void found \n");
return(RETURN);
}
'<=' {if (mydebug) fprintf(stderr, "void found \n");
return(LE);
}
'>=' {if (mydebug) fprintf(stderr, "void found \n");
return(GE);
}
'==' {if (mydebug) fprintf(stderr, "void found \n");
return(EQ);
}
'!=' {if (mydebug) fprintf(stderr, "void found \n");
return(NE);
}
[a-zA-Z][a-zA-Z0-9]* {if (mydebug) fprintf(stderr,"Letter found\n");
yylval.string=strdup(yytext); return(ID);}
[0-9][0-9]* {if (mydebug) fprintf(stderr,"Digit found\n");
yylval.value=atoi((const char *)yytext); return(NUM);}
[ \t] {if (mydebug) fprintf(stderr,"Whitespace found\n");}
[=\-+*/%&|()\[\]<>;] { if (mydebug) fprintf(stderr,"return a token %c\n",*yytext);
return (*yytext);}
\n { if (mydebug) fprintf(stderr,"cariage return %c\n",*yytext);
lineno++;
return (*yytext);}
%%
int yywrap(void)
{ return 1;}
最佳答案
您的词法分析器在行尾返回'\n'
(换行符)标记,但是您的解析器从不接受它们,因此当解析器到达第一个换行符时,您将收到语法错误。
关于syntax - YACC和LEX,在行尾出现语法错误,无法弄清原因,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49041453/
我正在用 lex 编写一个程序,它给了我以下错误: scan.l:49:无法识别的规则 第 49 行是:{number} {return(NUM);} 编辑: 但是,错误似乎与之前的行直接相关,{id
Lex 中的操作可以访问各个正则表达式组吗? (注意:我猜不是,因为组字符 - 括号 - 是根据 documentation 用于更改优先级的。但如果是这样,您是否推荐可以执行此操作的替代 C/C++
我对 lex 很陌生。我正在尝试开发一个解析器来搜索给定输入文件中特定单词的计数... 我的代码是 %{ #include #include #include int
如何使用 Lex/Yacc 识别汉字中的标识符? 最佳答案 我想你的意思是 Lex(词法分析器生成器)。 Yacc 是解析器生成器。 根据 What's the complete range for
我需要为不匹配的字符禁用 flex 输出。默认情况下,我无法禁用 ECHO(),也无法禁用 yyout。任何变体? 最佳答案 lex 的创建者提供了一种更简单、更易于维护的方法。在所有其他规则的末尾添
我想制作接受任何和所有条目的自定义插槽,只要这些条目遵循特定的正则表达式模式,例如:任意数量的字母或数字,但中间没有空格。谁能告诉我 amazon lex 中是否有实现它的方法?另外,如果我想获取某种
如何访问原始文本以引导 lex 中的意图 如何提取整个对话,包括用户输入和 Lex 响应 我想创建一个 lambda 和 API 网关来捕获输入并在将其发送到 Lex 之前记录它,并在每个意图 lam
我一直在研究具有多个意图的 AWS Lex 机器人。目前我遇到一个问题,插槽类型为 AMAZON.AlphaNumberic 的插槽只接受数字。 当我输入诸如“测试”之类的词时,它不会继续进入下一个槽
我正在尝试使用 AWS Lambda 的 C# 版本在列表中打印一系列对象。它链接到我的 AWS RDS 和我的 Lex 机器人。目前,当我使用 LEX 测试控制台测试机器人时,即使我使用 C# 的\
我对 Lex 和 Yacc 很陌生。我有一个 Lex 程序。示例:wordcount.l 我正在使用 windows 和 putty。 我只是想运行这个文件.. wordcount.l文件是否放在C盘
lex.yy.c 中的“yy”代表什么? 最佳答案 Lex 旨在与 Yacc 配合使用。 Steven Johnson 的论文 Yacc: Yet Another Compiler Compiler
Lex 和 Yacc 可以同时对 Lex 和 Yacc 进行 lex 和解析吗? 换句话说,是否可以编写一个自托管的 Lex/Yacc 组合,生成自己的解析器? 编辑:我并不是说组合需要完全解析输入的
可以在以下位置找到以下代码片段:http://zaach.github.io/jison/demos/calc/ ,以及 jison 文档页面。阅读 jison、lex 和 flex 文档后 - 我仍
我正在研究使用 boost::spirit::lex 编写词法分析器,但我能找到的所有示例似乎都假定您已先将整个文件读入 RAM。我想编写一个不需要整个字符串都在 RAM 中的词法分析器,这可能吗?或
我正在寻找通过 .NET SDK 或任何其他 API 构建和发布 AWS Lex 机器人的解决方案。 我们将不胜感激。 最佳答案 我最近刚刚使用 .NET SDK 开发了一个可用的 Amazon Le
编辑:我删除了词法分析器,因为它没有与 Qi 完全集成,只是混淆了语法(见下面的答案)。 我的词法分析器如下所示: template struct tokens : lex::lexer { tok
我为行数和字符数编写了一个 lex 程序(.l 文件) 程序: %{ int charcount=0,linecount=0; %} %% .charcount++ \n linecount++,
我正在尝试使用亚马逊的 boto3 构建一个聊天机器人图书馆。现在,我正在尝试使用 put_intent 创建一个意图。功能。我的代码如下: intent = lexClient.put_intent
在 ocamllex 中,我可以使用 _作为词法规则匹配任何与先前定义的规则不匹配的字符串,并引发错误。如何在 lex/flex 中实现这一点? 最佳答案 通常,您会定义这样的规则,它会放在最后: .
下载并安装了最新版本的 FlexSDK。将其解压到文件夹 C:FlexSDK 中。打开 C:/FlexSDK/bin 的命令提示符。运行该文件夹的目录,我的 mxmlc.exe 文件就在那里。在命令提
我是一名优秀的程序员,十分优秀!