Discussion:
Semantic rules not working
Saad Ahmed
2012-12-11 17:39:39 UTC
Permalink
Hi,
I have a grammar on which i am applying semantic rules. But my rules does
not seem to be fired . Here is an example .

compilation_unit
@init{
BufferedWriter out=null;
try{
// Create file
FileWriter fstream = new FileWriter("D:\\out.txt");
out = new BufferedWriter(fstream);
// out.write("Hello Java");
//Close the output stream
//out.close();
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
@after {
try {
out.write($str.text);
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

: (str=unit_statement)* EOF
;

unit_statement
returns [String str="Main Prodcution"]:
s1=create_procedure_body {$str+=$s1.text;}|
s2=create_package{System.out.println($s2.text);$str+=$s2.text;}
;
create_package
returns[String str=""]: c=create_key ( o=or_key r=replace_key )?
p=package_key
( p1=package_spec | p2=package_body )?
SEMICOLON? SOLIDUS //{$str=//$c.text+"kill"+$o.text+" changes made
here "+$p1.text;*/}
;

Now what is written on out.txt is the input stream as it is . No changes
that we have made in our semantic rules appear in the file ... Any help
would be appreciated.

List: http://www.antlr.org/mailman/listinfo/antlr-interest
Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address
John B. Brodie
2012-12-11 21:17:00 UTC
Permalink
just guessing here, but your changes appear to be in the str return
value, yet your references are all to the rule's text attribute (which
is the entire text recognized by the rule).

you might look at the generated java - is it very readable - and see
where your changes are placed - i suspect you are building an ast and a
str field is created in the rule's return class. so you might be able to
just change your .text references to .str.

but look at the generated code as a guide. try to figure out what ANTLR
is doing with your str return values.

hope this helps.
Post by Saad Ahmed
Hi,
I have a grammar on which i am applying semantic rules. But my rules does
not seem to be fired . Here is an example .
compilation_unit
@init{
BufferedWriter out=null;
try{
// Create file
FileWriter fstream = new FileWriter("D:\\out.txt");
out = new BufferedWriter(fstream);
// out.write("Hello Java");
//Close the output stream
//out.close();
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
@after {
try {
out.write($str.text);
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
: (str=unit_statement)* EOF
;
unit_statement
s1=create_procedure_body {$str+=$s1.text;}|
s2=create_package{System.out.println($s2.text);$str+=$s2.text;}
;
create_package
returns[String str=""]: c=create_key ( o=or_key r=replace_key )?
p=package_key
( p1=package_spec | p2=package_body )?
SEMICOLON? SOLIDUS //{$str=//$c.text+"kill"+$o.text+" changes made
here "+$p1.text;*/}
;
Now what is written on out.txt is the input stream as it is . No changes
that we have made in our semantic rules appear in the file ... Any help
would be appreciated.
List: http://www.antlr.org/mailman/listinfo/antlr-interest
Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address
List: http://www.antlr.org/mailman/listinfo/antlr-interest
Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address
Continue reading on narkive:
Loading...