softdevteam / grmtools

Rust grammar tool libraries and binaries
Other
507 stars 31 forks source link

Order of execution of grammar statements? #392

Closed madhavpcm closed 1 year ago

madhavpcm commented 1 year ago
    "INT_T" "MAIN" '('  ')' '{' LDeclBlock BeginBlock '}'
    {
        let mut ss = SCOPE_STACK.lock().unwrap();
        let mut fs = FUNCTION_STACK.lock().unwrap();
        fs.push("main".to_string());
        ss.push(HashMap::default().clone());

    log::info!("before size ofscope : {}", ss.len());
        std::mem::drop(ss);
        std::mem::drop(fs);

        log::info!("main()");
        let ldecl_ = $6.map_err(|_| ())?;
        __gen_local_symbol_table(&ldecl_, &ParamList::Null);
        let body_ = $7.map_err(|_| ())?;

        let node = ASTNode::MainNode{
            decl: Box::new(ldecl_),
            body: Box::new(body_),
        };

        let mut ss =SCOPE_STACK.lock().unwrap();
        log::info!("Local table of main() has {} elements", ss.last().unwrap().len());
        let mut ft = FUNCTION_TABLE.lock().unwrap();
        ft.insert(
            "main".to_string(),
            ss.last().unwrap().clone()
        );
    log::info!("in the end size ofscope : {}", ss.len());
        ss.pop();
        Ok(node)
    }
    ;
BeginBlock -> Result<ASTNode,()>:
    "BEGIN" StmtList "END" 
    {
        let ss = SCOPE_STACK.lock().unwrap();
        log::info!("Scope size for this begin block is {}", ss.len());
        std::mem::drop(ss);
        $2
    }
    | "BEGIN" "END" 
    {
        log::error!("Empty beginend");
        Ok(ASTNode::Null)
    }
    ;

I have this grammar to detect main function in a language and BeginBlock($7) contains the statement list. However I have a declaration block just before the beginblock called LDeclBlock($6). The problem which I face with this input is that, BeginBlock's actionbody is being executed before LDeclBlock's action body. Shouldnt LDeclBlocks action body be only called if I use let body = $7? in the action body ? So any local definitions are termed invalid while verifying body block (Because those variables are not pushed to the Local Symbol Table of the function). Is this supposed to happen with LR parsers? If not, am I missing something here?

//global declarations
decl
    int b;
    int c;
    int d;
enddecl
int main(){
       //local declarations
    decl
        int a,max;
    enddecl
    begin
        ...
        //the parse tree of begin-end block is generated before decl-enddecl block
        // that means use of variables a and max are invalidated by the invalidation function.
    end
}
ltratt commented 1 year ago

Can you cut the example down a bit so I can understand it more easily please?

madhavpcm commented 1 year ago

Ok, I have this grammar

    "INT_T" "MAIN" '('  ')' '{' LDeclBlock BeginBlock '}'
{
         ...
         process($6?) //LDeclBlock
         let node = $7?;  //BeginBlock
}
;

$6 contains grammar for local variables and process() function generates a table for storing the details of the local variables.

$7 should only be executed after process($6?) has been executed.

However when I run my code, $7 runs before process($6?). I don't know why this happens, is this an LR parsing thing or a mistake from my side? the process() is critical here.

ltratt commented 1 year ago

I'm really sorry but I'm going to struggle to debug with this sort of information. Either (preferably) I need a self-contained minimal example or (if that's not possible) a complete repository where I can compile and run code.

madhavpcm commented 1 year ago

https://github.com/madhavpcm/rexpl, here is the repository of the codebase im working on.

try cargo run -- input.expl in the src folder.

Also, when I use rust-gdb I cant goto the stack frames of the parser rust code.

in parser.y

//this is the intended order of execution
        let ldecl_ = $6.map_err(|_| ())?;
        __gen_local_symbol_table(&ldecl_, &ParamList::Null);
        let body_ = $7.map_err(|_| ())?;

however this gives the same output as the current state in the repo.

ltratt commented 1 year ago

I think I see what you're asking for:

$6 contains grammar for local variables and process() function generates a table for storing the details of the local variables.

$7 should only be executed after process($6?) has been executed.

In essence, all of the $ variables are assigned values before the user's code is executed. You can see this by putting log::info! calls into LDeclBlock and BeginBlock.

madhavpcm commented 1 year ago

I think I see what you're asking for:

$6 contains grammar for local variables and process() function generates a table for storing the details of the local variables. $7 should only be executed after process($6?) has been executed.

In essence, all of the $ variables are assigned values before the user's code is executed. You can see this by putting log::info! calls into LDeclBlock and BeginBlock.

Yes I just noticed it. I misinterpreted some things and apologize for the confusion. I may have figured out a fix for my situation.

In essence, all of the $ variables are assigned values before the user's code is executed. You can see this by putting log::info! calls into LDeclBlock and BeginBlock.

Can we override this somehow ? Otherwise I think this issue can be closed.

Thanks Alot again!,

ltratt commented 1 year ago

Can we override this somehow ?

There's no current mechanism, and it would be difficult to do so: grmtools would more-or-less need to read your mind!

FWIW, my preference is not to do as much in action code as you're doing. I prefer to create a minimal Abstract Syntax Tree in action code, and then have a later step which translates the AST into something else. This gives me much more control over evaluation order, and allows me to do things in a different order than dictated by the grammar. You can see a simple example of this in pizauth (https://github.com/ltratt/pizauth/blob/master/src/config.y, https://github.com/ltratt/pizauth/blob/master/src/config_ast.rs, https://github.com/ltratt/pizauth/blob/master/src/config.rs) or a more complex example in yksom (https://github.com/softdevteam/yksom/tree/master/src/lib/compiler).

Best of luck!