Replies: 4 comments 2 replies
-
Case 2 doesn't work because:
|
Beta Was this translation helpful? Give feedback.
-
Thanks, kaby- this is working but I have some questions. I apologize if some of these seem trivial - parts of your solution run contradictory to my understanding of some ANTLR concepts. Question 1What factors motivate explicit definition of tokens in this manner? tokens {
EXEC_SQL_STMT,
EXEC_SQL_EXECUTE_STMT
} I looked to Question 2Why is it necessary to handle whitespace explicitly using Question 3Lexer rule EXEC_SQL_START: 'EXEC' [ \t\n\r]+ 'SQL' -> pushMode(EXEC_SQL), more;
EXEC_SQL_EXECUTE_START: 'EXEC' [ \t\n\r]+ 'SQL' [ \t\n\r]+ 'EXECUTE' -> pushMode(EXEC_SQL_EXECUTE), more; Thanks again, |
Beta Was this translation helpful? Give feedback.
-
The You could write a rule As for your third question, the order of rules almost always should not matter. Antlr always matches the longest string. For input |
Beta Was this translation helpful? Give feedback.
-
This was immensely helpful - thank you, kaby! |
Beta Was this translation helpful? Give feedback.
-
What is the best method for switching lexer modes when the start and end tokens are not simple characters like
<
>
in XML?Background
I'm using ANTLR to parse C code containing embedded PlSql - the language is Oracle Pro*C. My goal is to parse the outer grammar as regular C and use modes for capturing embedded Pl/Sql as arbitrary text for parsing in a separate process.
##Test
For the sake of simplicity I'm using ExprParser/ExprLexer as the starting point just to get modes working.
Results so far
Pro*C has various but distinct start and end tags for "inside content" - similar to XML, though not always single character tokens:
CASE 1
Simple scenario: Embedded block starts with
EXEC
SQL
, followed by legit arbitrary SQL, ends with;
I got CASE 1 working with the following adjustments:
Case 2
Embedded block starts with
EXEC
SQL
EXECUTE
, followed by anonymous PL/Sql (not just Sql), ends withEND-EXEC;
Problems &Questions
Mode
SQLEXEC
triggers onEXEC SQL EXECUTE
, butSqlExecText
consumes the end tagEND-EXEC;
before popping back to default mode - this is not a surprise since unlike the previous scenario I can't exclude the end tag from theSqlExecText
rule - this is where I'm stuck.Questions
CLOSE_SQLEXEC
trigger popMode() given its priority over other rules in mode SQLEXEC?Potential Options
TokenSource
for manipulating/combining tokens before they hitTokenStream
, but I don't know enough about it yet to determine if it's worth pursuing.Beta Was this translation helpful? Give feedback.
All reactions