yacc

学习编写编译器[关闭]

谁说胖子不能爱 提交于 2019-12-23 19:45:30
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 首选语言 :C / C ++,Java和Ruby。 我正在寻找一些有用的书籍/教程,以了解如何仅出于教育目的编写自己的编译器。 我最熟悉C / C ++,Java和Ruby,因此我更喜欢涉及这三种资源之一的资源,但是任何好的资源都是可以接受的。 #1楼 这是一个广阔的主题。 不要小看这一点。 并且不要低估我的观点,不要低估它。 我听说《 龙书》 是一个(“?”)学习的起点。 :)善于搜索,最终将成为您的生活。 构建自己的编程语言绝对是一个好练习! 但是要知道,最终它永远不会用于任何实际目的。 例外情况很少, 而且 相差 很 远。 #2楼 您可能想要研究Lex / Yacc(或Flex / Bison,无论您想称呼它们如何)。 Flex是一个词法分析器,它将分析和识别您语言的语义成分(“令牌”),而Bison将用于定义解析每个令牌时发生的情况。 对于可以编译为C的编译器或动态运行指令,这可能是但绝对不限于打印C代码。 该常见问题解答 应为您提供帮助, 本教程 看起来非常有用。 #3楼 我认为这是一个非常模糊的问题。 只是因为涉及的话题很深。 但是,编译器可以分解为两个独立的部分。 上半部分和下半部分。 上半部通常采用源语言并将其转换为中间表示,下半部负责平台特定的代码生成。 尽管如此

How do I generate different yyparse functions from lex/yacc for use in the same program?

╄→尐↘猪︶ㄣ 提交于 2019-12-23 18:55:42
问题 I want to generate two separate parsing functions from lex/yacc. Normally yacc gives you a function yyparse() that you can call when you need to do some parsing, but I need to have several different yyparses each associated with different lexers and grammars. The man page seems to suggest the -p (prefix) flag, but this didn't work for me. I got errors from gcc that indicated that yylval was not properly being relabeled (i.e. it claims that several different tokens are not defined). Does

yylineno has always the same value in yacc file [duplicate]

独自空忆成欢 提交于 2019-12-23 18:53:15
问题 This question already has an answer here : Flex yylineno set to 1 (1 answer) Closed 4 years ago . for one project in compilers i have one problem in the syntax analyzer, when i go to add a symbol in a symbol table, i take always the same value in yylineno... i did this in the begining: %{ int yylex(void); int yyerror(char* yaccProvidedMessage); extern int yylineno; //i declare yylineno from the lexical analyzer extern char *yytext; extern FILE *yyin; int scope=0; int max_scope; %} and in the

How to tokenize String in Lex and Yacc

谁说我不能喝 提交于 2019-12-23 05:20:20
问题 with reference to Reading new line giving syntax error in LEX YACC lex file we are using %{ /* parser for ssa; */ #include<stdio.h> #include<stdlib.h> #include"y.tab.h" %} %% [\t]+ ; \n ; [if]+ printf("first input\n"); [else]+ return(op); [=]+ return(equal); [+]+ return(op); [*]+ return(op); [-]+ return(op); [\<][b][b][ ]+[1-9][\>] {return(bblock);} ([[_][a-z]])|([a-z][_][0-9]+)|([0-9]+) {return(var);} . ; %% what should i do if i want to get token as a string i.e a_2 how to do it???? input

How can I send the yyleng of a matched string from Lex to Yacc?

纵饮孤独 提交于 2019-12-23 03:09:10
问题 Please i am trying to pass the yyleng of a matched string from my (.l) file to the (.y) file. Here is a sample of the issue: In the Lex File: <state1>.+ { fprintf(yyout, "%d", yyleng); } In the Yacc File: /* I need to know the methodology used to receive a specific yyleng to the yacc file. Shall I use global variables? or there is a specific way for dealing with this issue? */ Thanks in advance for your help! ~ Any suggestions are highly appreciated. 回答1: yyleng is a global variable; declare

Cyclic Dependency in reentrant flex / bison headers with union YYSTYPE

戏子无情 提交于 2019-12-23 02:34:38
问题 I have a problem where I believe there is a cyclic dependency between the headers generated by flex and bison . The type yyscan_t is defined in the lex header and needed in the yacc header. The macro YYSTYPE is defined in the yacc header and needed in the lex header. No matter which order I import the two headers, the other will not be happy. reentrant.lex: %{ #include "reentrant.yacc.h" %} %option reentrant bison-bridge %% [0-9]+ { yylval->int_value = atoi(yytext); return INT_TERM; } [a-zA-Z

Why are these conflicts appearing in the following yacc grammar for XML

隐身守侯 提交于 2019-12-22 13:07:51
问题 I have the following XML grammar which works fine: program : '<' '?'ID attribute_list '?''>' root ; root : '<' ID attribute_list '>' node_list '<''/'ID'>' ; node_list : node_s | node_list node_s ; node_s : node | u_node | ID ; node : '<' ID attribute_list '/''>' ; u_node :'<' ID attribute_list '>' node_list '<''/'ID'>' |'<' ID attribute_list '>' '<''/'ID'>' ; attribute_list : attributes | ; attributes : attribute | attributes attribute ; attribute : ID ASSIGNOP '"' ID '"' | ID ASSIGNOP '"'

Why are these conflicts appearing in the following yacc grammar for XML

江枫思渺然 提交于 2019-12-22 13:07:14
问题 I have the following XML grammar which works fine: program : '<' '?'ID attribute_list '?''>' root ; root : '<' ID attribute_list '>' node_list '<''/'ID'>' ; node_list : node_s | node_list node_s ; node_s : node | u_node | ID ; node : '<' ID attribute_list '/''>' ; u_node :'<' ID attribute_list '>' node_list '<''/'ID'>' |'<' ID attribute_list '>' '<''/'ID'>' ; attribute_list : attributes | ; attributes : attribute | attributes attribute ; attribute : ID ASSIGNOP '"' ID '"' | ID ASSIGNOP '"'

Resolving reduce/reduce conflict in yacc/ocamlyacc

丶灬走出姿态 提交于 2019-12-22 03:52:40
问题 I'm trying to parse a grammar in ocamlyacc (pretty much the same as regular yacc) which supports function application with no operators (like in Ocaml or Haskell), and the normal assortment of binary and unary operators. I'm getting a reduce/reduce conflict with the '-' operator, which can be used both for subtraction and negation. Here is a sample of the grammar I'm using: %token <int> INT %token <string> ID %token MINUS %start expr %type <expr> expr %nonassoc INT ID %left MINUS %left APPLY

Resolving reduce/reduce conflict in yacc/ocamlyacc

谁说胖子不能爱 提交于 2019-12-22 03:52:10
问题 I'm trying to parse a grammar in ocamlyacc (pretty much the same as regular yacc) which supports function application with no operators (like in Ocaml or Haskell), and the normal assortment of binary and unary operators. I'm getting a reduce/reduce conflict with the '-' operator, which can be used both for subtraction and negation. Here is a sample of the grammar I'm using: %token <int> INT %token <string> ID %token MINUS %start expr %type <expr> expr %nonassoc INT ID %left MINUS %left APPLY