javacc

Javacc Unreachable Statement

两盒软妹~` 提交于 2019-12-09 23:11:06
问题 In my grammar there are production rules for expressions and fragments which originally contained indirect left recursion. This is the rules after I removed the recursion from them. String expression() #Expression : {String number; Token t;} { number = fragment() ( (t = <Mult_Sign> number = fragment()) ) {return number;} } String fragment() #void : {String t;} { t = identifier() {return t;} | t = number() {return t;} | (<PLUS> | <MINUS> ) fragment() | <LBR> expression() <RBR> } These

Make a calculator's grammar that make a binary tree with javacc

北城余情 提交于 2019-12-09 03:51:04
问题 I need to make a simple calculator (with infix operator) parser that handle the operators +,-,*,/ and float and variable. To make this I used javacc, and I have made this grammar with jjtree. It works but it doesn't ensure that the final tree will be a binary tree, which I need. I want something like 5*3+x-y to generate the following tree : * / \ 5 + / \ 3 - / \ x y What would be a proper grammar to do that, that would not be left-recursive ? 回答1: Something like the following will give you

Left recursion elimination in an LL1 grammar

寵の児 提交于 2019-12-08 08:39:07
问题 I'm trying to eliminate left recursion from the following extract of a grammar - expression := fragment ( ( + | - | * | / ) fragment )* fragment := identifier | number | ( + | - ) fragment | expression The issue is that expression can go to fragment, can go to expression. I've tried a bunch of ways to eliminate it, some look like they work (in JavaCC) but I'm a)unsure of their correctness, and b) pretty sure I've broken associativity by changing the structure of the grammar. I'm pretty sure I

LOOKAHEADs for the JavaScript/ECMAScript array literal production

左心房为你撑大大i 提交于 2019-12-08 04:11:23
问题 I currently implementing a JavaScript/ECMAScript 5.1 parser with JavaCC and have problems with the ArrayLiteral production. ArrayLiteral : [ Elision_opt ] [ ElementList ] [ ElementList , Elision_opt ] ElementList : Elision_opt AssignmentExpression ElementList , Elision_opt AssignmentExpression Elision : , Elision , I have three questions, I'll ask them one by one. This is the second one. I have simplified this production to the following form: ArrayLiteral: "[" ("," | AssignmentExpression ","

Left Factoring & Removing Left Recursion JavaCC

一世执手 提交于 2019-12-08 01:30:21
问题 I have a grammar which I have to use JJTree and JavaCC to create a symbol table and an AST. While I fully understand the sections of my assignment to create the table and tree, the grammar I was given is ambiguous, contains left recursion and indirect left recusion. It also needs to be left factored. I have trawled all over the internet to try find methods that would work for me. For example: A ::= Aα | β can be changed to: A ::= βA' A' ::= αA' | ε But I don't know how to apply this to my

parsing and evaluating simple language using javacc

蓝咒 提交于 2019-12-07 13:27:25
问题 I have simple language like: funa X ( X+3*funb(1) ) ; funb Y ( 2*Y ) ; main ( 2+func(func(1)) ) ; func A ( funa(A) ) ; I used CFG to parse above as: program => (statement)+ statement => (<main_keyword> | <idl> <idu>) <lparan> expression <rparan> <semicolon> expression => T(<plus> T)* T => P(<multipliation>P)* P => <idu> | <idl> <lparan> expression <rparan> | <number> And token recognizing: <main_keyword> -> "main" <idl> -> (["a"-"z"])+ <idu> -> (["A"-"Z"])+ <lparan> -> "(" <rparan> -> ")"

freemarker源码解读之一--概述

谁说我不能喝 提交于 2019-12-07 08:00:53
最近在思考为如何xsoup添加自定义函数支持,基于这个目的,想起了最常用的模板引擎freemarker。于是down了源码下来,开始浏览一番。本文基于 https://github.com/freemarker/freemarker 上的2.3.20版本。 思考 看源码前,先思考一下,一个模板引擎,到底需要哪些东西?一门模板引擎其实是一个完整的语言,只不过它只具有单纯的输入/输出,不需要考虑其他的功能。 语法解析,转换为AST(抽象语法树) 语义分析,为AST附加上执行语义 上下文环境的注入 内置函数及外部函数支持 其他外围机制(与框架/工具的集成等) 源码结构 打开freemarker的代码,core包里110个类一字排开,还有以下划线开头的“建议不要看”的类,看的人眼花缭乱啊!这一切都是因为Java规范的大师们,设计了一个“包级可见“的概念,大概就是,我写给自己用的代码,不要让你用!这一来,别人用是用不了了,好像看起来也变得很困难了… 还好现在的IDE都很强大,刷刷两下就给重构了,把一些类按照类型挪到多个包里,顿时清爽很多!可惜好多类/方法/字段都是包级可见,为了让这个重构版freemarker没那么多红叉,lz加了好几百个public,写到意识都模糊了…最后把自己的劳动成果共享出来吧: https://github.com/code4craft/freemarker

Left recursion elimination in an LL1 grammar

孤街醉人 提交于 2019-12-06 16:06:20
I'm trying to eliminate left recursion from the following extract of a grammar - expression := fragment ( ( + | - | * | / ) fragment )* fragment := identifier | number | ( + | - ) fragment | expression The issue is that expression can go to fragment, can go to expression. I've tried a bunch of ways to eliminate it, some look like they work (in JavaCC) but I'm a)unsure of their correctness, and b) pretty sure I've broken associativity by changing the structure of the grammar. I'm pretty sure I need an expression', and have fragment := identifier | number | ( + | - ) fragment | expression

Left Factoring & Removing Left Recursion JavaCC

∥☆過路亽.° 提交于 2019-12-06 04:57:34
I have a grammar which I have to use JJTree and JavaCC to create a symbol table and an AST. While I fully understand the sections of my assignment to create the table and tree, the grammar I was given is ambiguous, contains left recursion and indirect left recusion. It also needs to be left factored. I have trawled all over the internet to try find methods that would work for me. For example: A ::= Aα | β can be changed to: A ::= βA' A' ::= αA' | ε But I don't know how to apply this to my grammar. Here is a section of the production rules I wrote from the grammar that contains the problems

parsing and evaluating simple language using javacc

天大地大妈咪最大 提交于 2019-12-05 22:44:42
I have simple language like: funa X ( X+3*funb(1) ) ; funb Y ( 2*Y ) ; main ( 2+func(func(1)) ) ; func A ( funa(A) ) ; I used CFG to parse above as: program => (statement)+ statement => (<main_keyword> | <idl> <idu>) <lparan> expression <rparan> <semicolon> expression => T(<plus> T)* T => P(<multipliation>P)* P => <idu> | <idl> <lparan> expression <rparan> | <number> And token recognizing: <main_keyword> -> "main" <idl> -> (["a"-"z"])+ <idu> -> (["A"-"Z"])+ <lparan> -> "(" <rparan> -> ")" <semicolon> -> ";" <number> -> (["0"-"9"])+ <plus> -> "+" <multiplication> -> "*" I am able to parse above