javacc

JavaCC quote with escape character

為{幸葍}努か 提交于 2019-12-13 14:55:34
问题 What is the usual way of tokenizing quoted strings that can contain an escape character? Here are some examples: 1) "this is good" 2) "this is\"good\"" 3) "this \is good" 4) "this is bad\" 5) "this is \\"bad" 6) "this is bad 7) this is bad" 8) this is bad Below is a sample parser that doesn't work quite right; it has expected results for all except examples 4 and 5, which parse successfully. options { LOOKAHEAD = 3; CHOICE_AMBIGUITY_CHECK = 2; OTHER_AMBIGUITY_CHECK = 1; STATIC = false; DEBUG

Print in JavaCC

帅比萌擦擦* 提交于 2019-12-13 08:18:06
问题 I made a simple javacc parser to print text in the command prompt: options { static = true; } PARSER_BEGIN(Printer) package test2; import java.io.*; public class Printer { public static void main(String args []) throws ParseException { Printer parser = new Printer(System.in); parser.parse(System.out); } } PARSER_END(Printer) SKIP: { "\n" | "\r" | "\r\n" } TOKEN:{ < PRINT: "print" > | < STR: (["a"-"z"] | ["A"-"Z"])* > } void parse(PrintStream ps) : { Token t; } { (< PRINT > " " t = < STR > {

how do I implement loops (For) in javacc

懵懂的女人 提交于 2019-12-13 07:34:29
问题 I made an interpreter for a script language containing a loop for, using javacc I have defined the grammar but I can't have a way to back up to line to repeat the execution of the block "for". how back up the token manager so that loop-bodies can be re-parsed, and thus reevaluated, over and over again ?. void For(): {ArrayList<String> lst;Token n,v;int i=0;} { "for" "(" n=<ID> ":" v=<ID> ")" "{" (actions()";" )+ "}" } 回答1: As explained in the JavaCC FAQ (http://www.engr.mun.ca/~theo/JavaCC

Java: Stackoverflow in finite recursion

妖精的绣舞 提交于 2019-12-12 04:27:59
问题 I wrote a javaCC parser for some propositional logic expressions. The expressions can get pretty long, 30k many characters. When I parse such big expressions, I get a stack-overflow exception. Is there maybe some VM parameter which determines the stack size? Or what would you do in such cases? Thanks 回答1: Yes, use the -Xss parameter. e.g.: java -Xss4m Blah sets the stack-size to 4MB. 回答2: While you can alter the stack size as per Oli's answer, I would suggest you try to look for an

How to create specific error messages based on parse exceptions in JavaCC

北城余情 提交于 2019-12-12 01:35:48
问题 I'm making a JavaCC program to accept a certain language. I've done this but cannot understand how to use a generated ParseException to determine the issue in the input, and customise the output error message. So far my code looks like: try { task parser = new task(System.in); parser.start(); System.out.println("YES"); // If accepted print YES. } catch (ParseException e) { System.out.println("NO"); // If rejected print NO. switch (e) { case 1: System.err.println("Some error case") case 2: ...

How to resolve conflict between two choices starting with same tokens in javacc

可紊 提交于 2019-12-11 12:17:47
问题 I'm trying to write a compiler for some specific format of messages. My problem if I simplify it is: < WORD : ([LETTER]){2,5}> < ANOTHER_WORD : (<LETTER>|<DIGIT>){1,5}> < SPECIAL_WORLD : "START"> void grammar(): { } { <WORD><ANOTHER_WORD> | <SPECIAL_WORD><ANOTHER_WORD> } Here my special word is matched always as a WORD which is logical of course but since the conflict is at the beginning of the production I don't know how to resolve it. some help would be appreciated. 回答1: Put the rule for

Java CC issue - “Expansion within ”(…)*“ can be matched by empty string”

删除回忆录丶 提交于 2019-12-11 12:05:58
问题 We've been given a grammar to patch up and parse using Java CC. One of the problems with it is several occurrences of "expansion within "(...)*" can be matched by empty string. I understand this error is caused when something can be matched zero or more times inside something else that can be matched zero or more times. What I don't understand is how to fix it. (Our instructor hasn't been saying much, "You have to be careful how you word it." The problem area of the grammar, along with its

Exclude a string as a token in Javacc

荒凉一梦 提交于 2019-12-11 04:11:31
问题 Im a trying to create a grammar in Javacc, and for a function name, I can have any assortment of lower character letters, or 'main'. If 'main' is the function name, then different procedures needed to be taken. My problem is that main can not be matched as a string literal when it is included as a choice, and is matched to instead. So I was hoping that if I exclude the word 'main' from the token, then it might work! < FUNCNAME: (["a"-"z"])+ ~["main"]> 回答1: The easiest thing to do is to make

Could not load main class in JavaCC

时光毁灭记忆、已成空白 提交于 2019-12-11 02:46:09
问题 I am AI student and we work with JavaCC. I am new with it. I was trying simple example and I had some errors. 1) I downloaded JavaCC 0.6 from it's website 2) I extracted it in disc C 3) I wrote this code in a file with extension ".jj" PARSE_BEGIN(Test) import java.io.*; class Test { public static void main(string [] args) { new Test(new InputStreamReader(System.in)); start(); } } PARSE_END(Test); Token: { <number: (["0"-"9"])+("." (["0"-"9"])+)?(("e"|"E")(["0"-"9"])+)?>| <plus: "+"> } void

JavaCC lexical error on any type of whitespace

喜欢而已 提交于 2019-12-10 09:24:43
问题 I cleary have the unicode whitespace characters defined in my SKIP token like so: SKIP { " " | "\r" | "\n" | "\t" } However, when I run Java CC it parses all the tokens fine until I hit any of the above mentioning white space characters and it throws the following error: Exception in thread "main" prjct1.TokenMgrError: Lexical error at line 1, column 25. Encountered: "\r" (13), after : "Random:Word:Here" So as you can see it runs fine until it hits the "\r". I get the same error with " ", "\n