lex

REPL for interpreter using Flex/Bison

断了今生、忘了曾经 提交于 2019-12-30 03:11:46
问题 I've written an interpreter for a C-like language, using Flex and Bison for the scanner/parser. It's working fine when executing full program files. Now I'm trying implement a REPL in the interpreter for interactive use. I want it to work like the command line interpreters in Ruby or ML: Show a prompt Accept one or more statements on the line If the expression is incomplete display a continuation prompt allow the user to continue entering lines When the line ends with a complete expression

Parsing an equation with sub-formulas in python

霸气de小男生 提交于 2019-12-25 10:22:40
问题 I'm trying to develop an equation parser using a compiler approach in Python. The main issue that I encounter is that it is more likely that I don't have all the variables and need therefore to look for sub-formulas. Let's show an example that is worth a thousand words ;) I have four variables whom I know the values: vx, vy, vz and c: list_know_var = ['vx', 'vy', 'vz', 'c'] and I want to compute the Mach number (M) defined as equation = 'M = V / c' I already know the c variable but I don't

Parsing an equation with sub-formulas in python

亡梦爱人 提交于 2019-12-25 10:21:33
问题 I'm trying to develop an equation parser using a compiler approach in Python. The main issue that I encounter is that it is more likely that I don't have all the variables and need therefore to look for sub-formulas. Let's show an example that is worth a thousand words ;) I have four variables whom I know the values: vx, vy, vz and c: list_know_var = ['vx', 'vy', 'vz', 'c'] and I want to compute the Mach number (M) defined as equation = 'M = V / c' I already know the c variable but I don't

Character Position from starting of a line

偶尔善良 提交于 2019-12-25 03:01:44
问题 In flex, what's the way to get character position from the starting of a line? I've got a post regarding position from start of a file but i want it from the start of a line. Also it should handle case like this: /** this is a comment */int x,y; output: Here position of "int"=3 Please give me some hints to implement this. 回答1: I presume that the "post regarding position from start of a file" is this one, or something similar. The following is based on that answer. To track the current column

ambiguity in parsing comma as a operator using PLY python

六眼飞鱼酱① 提交于 2019-12-25 01:34:34
问题 I have following tokens and many more, but I want to keep my question short that's why not including the whole code. tokens = ( 'COMMA', 'OP', 'FUNC1', 'FUNC2' ) def t_OP(t): r'&|-|\||,' return t def t_FUNC1(t): r'FUNC1' return t def t_FUNC2(t): r'FUNC2' return t Other methods: def FUNC1(param): return {'a','b','c','d'} def FUNC2(param,expression_result): return {'a','b','c','d'} My grammar rules in YACC are and few more are there but listed important ones: 'expression : expression OP

How to get more parse error information from lex / yacc?

有些话、适合烂在心里 提交于 2019-12-24 22:32:10
问题 How to get more parse error information from lex / yacc? Currently in the lex file I am using: int yyerror(const char *msg) { fprintf(stderr, "Parse error: %s\n", msg); return 0; } But when I run my program yyerror outputs a blank message. I tried adding yylineno and yytext to yyerror but these cause compilation errors. I tried adding "%error-verbose" to the yacc file and "%option debug" to the lex file but these made no difference to the message. I did notice however that yyparse outputs a

Yacc 与 Lex 快速入门

情到浓时终转凉″ 提交于 2019-12-23 21:21:45
developerWorks 中国 > Linux > Yacc 与 Lex 快速入门 Lex 与 Yacc 介绍 文档选项 打印本页 将此页作为电子邮件发送 级别: 初级 Ashish Bansal ( mailto:abansal@ieee.org?subject=Yacc 与 Lex 快速入门&cc=abansal@ieee.org ), 软件工程师, Sapient 公司 2000 年 11 月 01 日 Lex 和 Yacc 是 UNIX 两个非常重要的、功能强大的工具。事实上,如果你熟练掌握 Lex 和 Yacc 的话,它们的强大功能使创建 FORTRAN 和 C 的编译器如同儿戏。Ashish Bansal 为您详细的讨论了编写自己的语言和编译器所用到的这两种工具,包括常规表达式、声明、匹配模式、变量、Yacc 语法和解析器代码。最后,他解释了怎样把 Lex 和 Yacc 结合起来。 Lex 代表 Lexical Analyzar。Yacc 代表 Yet Another Compiler Compiler。 让我们从 Lex 开始吧。 Lex Lex 是一种生成扫描器的工具。扫描器是一种识别文本中的词汇模式的程序。这些词汇模式(或者常规表达式)在一种特殊的句子结构中定义,这个我们一会儿就要讨论。 一种匹配的常规表达式可能会包含相关的动作。这一动作可能还包括返回一个标记

学习编写编译器[关闭]

谁说胖子不能爱 提交于 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

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