Please, I need some help using Irony to replace text

Oct 29, 2012 at 11:51 AM

Hello All,

I am a newbie in both Irony and parsing. I need some help to find the right approach in a case. I created a Grammar for SQL select statements. Everything is working and I am quite impressed with the expressivenes of Irony (I love using C# to express my grammar). Now I would like to use Irony and replace dynamically some text.

Lets say I parse "SELECT A, B, C FROM TABLE1, TABLE2". I would like to replace the field A with a new text (say REAL_A). What is the best way for doing it? I want to identify the fields and only do this replacement for field names and not for tables.

My grammar has got "Identifier" as a NonTerminal and this is used for field names and table names. Now I would like to intercept the parsing of identifiers that are used for field names and then transform the content using some internal rules.

Is the Reduced event for the NonTerminals the right one? How could I see if the "Identifier" was in a field list or a table list? How could I replace the text with new one?

Sorry for the many questions

RaulRG

Oct 30, 2012 at 12:34 AM

Don't you just want to walk the ParseTree?

Coordinator
Oct 30, 2012 at 5:51 AM

Agree, you need a separate process, after parsing, working on parse tree and changing it appropriately. This is in fact semantic analysis. Then finally output the text from the tree. Hooking to Reduced event obviously won't make it - when you reduce field list in SELECT clause, the FROM clause is still ahead, so you don't know the table name yet.

Roman

Oct 30, 2012 at 7:56 AM

Thank you for your answers,

if this is the way of course I can walk the ParseTree. In fact, I show the tree as you do in your GrammarExplorer. For me it was important to know if this is the way of doing it. I discovered the Reduced event and it looked as if I could use it for it. This could have been very elegant for solving the matter.

@Roman: in my case I don't need to know the tables for replacing the field names. I've got a kind of mapping subsystem and I just need to get the names to map them to the real one.