Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Defining string java cup/jflex

Reply
Thread Tools

Defining string java cup/jflex

 
 
lionelv@gmail.com
Guest
Posts: n/a
 
      02-08-2007
Maybe someone here can help with this.

I'm a newbie to cup/jflex and I'm having some troubles. I'm starting
with an example I found somewhere and modifying it slowly to get the
desired result. I want to allow strings, eventually between braces
{aString} but for now I've been trying between quotes as the examples
do it this way. In the following "comment" is a keyword:

4+8;
comment "aString";

Here is the code that I have modified:

Scanner.jflex

*************************************
package Example;

import java_cup.runtime.SymbolFactory;
%%
%cup
%class Scanner
%{
public Scanner(java.io.InputStream r, SymbolFactory sf){
this(r);
this.sf=sf;
}
StringBuffer string = new StringBuffer();
private SymbolFactory sf;
%}
%eofval{
return sf.newSymbol("EOF",sym.EOF);
%eofval}

%state STRING

%%
";" { return sf.newSymbol("Semicolon",sym.SEMI); }
"+" { return sf.newSymbol("Plus",sym.PLUS); }
"*" { return sf.newSymbol("Times",sym.TIMES); }
"(" { return sf.newSymbol("Left Bracket",sym.LPAREN); }
")" { return sf.newSymbol("Right Bracket",sym.RPAREN); }
[0-9]+ { return sf.newSymbol("Integral Number",sym.NUMBER, new
Integer(yytext())); }
[ \t\r\n\f] { /* ignore white space. */ }
.. { System.err.println("Illegal character: "+yytext()); }

\" { string.setLength(0); yybegin(STRING); }

<YYINITIAL> "comment" { return sf.newSymbol("Comment", sym.COMMENT); }

<STRING> {
\" { yybegin(YYINITIAL);
return symbol(sym.STRING_LITERAL,
string.toString()); }
[^\n\r\"\\]+ { string.append( yytext() ); }
\\t { string.append('\t'); }
\\n { string.append('\n'); }

\\r { string.append('\r'); }
\\\" { string.append('\"'); }
\\ { string.append('\\'); }
}

*************************************

Parser.cup

*************************************
package Example;

import java_cup.runtime.*;

parser code {:
public static void main(String args[]) throws Exception {
SymbolFactory sf = new ComplexSymbolFactory();
if (args.length==0) new Parser(new
Scanner(System.in,sf),sf).parse();
else new Parser(new Scanner(new
java.io.FileInputStream(args[0]),sf),sf).parse();
}
:}

terminal COMMENT;
terminal String STRING;
terminal SEMI, PLUS, TIMES, LPAREN, RPAREN;
terminal Integer NUMBER;

non terminal expr_list, expr_part, type_data;
non terminal Integer expr;

precedence left PLUS;
precedence left TIMES;

expr_list ::= expr_list expr_part | expr_part | expr_list type_data;
expr_part ::= expr:e {: System.out.println(" = "+e+";"); :} SEMI;
expr ::= NUMBER:n
{: RESULT=n; :}
| expr:l PLUS expr:r
{: RESULT=new Integer(l.intValue() + r.intValue()); :}
| expr:l TIMES expr:r
{: RESULT=new Integer(l.intValue() * r.intValue()); :}
| LPAREN expr:e RPAREN
{: RESULT=e; :}
;
type_data ::= COMMENT {: System.out.println("bar"); :} STRING:str {:
System.out.println(str); :} SEMI;

*************************************

The above code once I run it on the lines a gave gives a syntax error
and complains about illegal characters " a S t r i n g and "

What am I doing wrong and where should I start learning about this?

thanks

Lionel.

 
Reply With Quote
 
 
 
 
Oliver Wong
Guest
Posts: n/a
 
      02-12-2007

<> wrote in message
news: ps.com...
> Maybe someone here can help with this.
>
> I'm a newbie to cup/jflex and I'm having some troubles. I'm starting
> with an example I found somewhere and modifying it slowly to get the
> desired result. I want to allow strings, eventually between braces
> {aString} but for now I've been trying between quotes as the examples
> do it this way.

[code snipped]
>
> The above code once I run it on the lines a gave gives a syntax error
> and complains about illegal characters " a S t r i n g and "
>
> What am I doing wrong and where should I start learning about this?


Is your goal to learn cup/jflex, or is it to write your own parser? If
it's the latter, I recommend you use a different parser generator, since
from the lack of response, it sounds like most people don't know cup/jflex's
syntax. Try ANTLR, for example. They have a very active mailing list and so
someone will probably answer your questions.

If it's the former... well... good luck.

- Oliver


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Rational argument for not defining string constant for empty string? david.karr Java 21 03-14-2010 07:39 PM
how to change a value of a variable defining in code behind via Java Script dhmplopoka ASP .Net 0 10-20-2008 02:35 PM
Defining a new class similar to String Bernhard Enders Java 1 09-01-2006 09:04 PM
Defining a string/array with perl characters Duke of Hazard Perl Misc 0 09-28-2004 02:10 AM
defining or not defining destructors johny smith C++ 8 07-02-2004 08:51 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57