Got some error

May 7, 2014 at 12:02 PM
Edited May 7, 2014 at 12:06 PM
Hi I am building a simple language but having problem to parse
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using Irony.Parsing;

[Language("Short Script")]
public class ShortGrammer : Grammar
    public ShortGrammer() : base(false)
        dynamic KeyWords = {
        IdentifierTerminal Identifier = default(IdentifierTerminal);
        StringLiteral String = default(StringLiteral);
        NumberLiteral Number = default(NumberLiteral);
        String = TerminalFactory.CreateVbString("String");
        Number = TerminalFactory.CreateVbNumber("Number");
        Identifier = TerminalFactory.CreateCSharpIdentifier("Identifier");
        //Non Terminals
        NonTerminal Program = new NonTerminal("Program");
        NonTerminal ProgramStatements = new NonTerminal("ProgramStatements");
        NonTerminal gVarStatements = new NonTerminal("gVarStatements");
        NonTerminal ProcedureStatements = new NonTerminal("ProcedureStatements");
        NonTerminal ArgStatements = new NonTerminal("ArgStatements");
        NonTerminal Stmnts = new NonTerminal("Stmnts");
        NonTerminal Values = new NonTerminal("Values");
        NonTerminal gVarStatement = new NonTerminal("gVarStatement");
        NonTerminal ProgramStatement = new NonTerminal("ProgramStatement");
        NonTerminal ProcedureStatement = new NonTerminal("ProcedureStatement");
        NonTerminal ProgKeyWord = new NonTerminal("ProgKeyWord");
        NonTerminal Stmnt = new NonTerminal("Stmnt");
        NonTerminal Value = new NonTerminal("Value");
        Program.Rule = ProgramStatements + gVarStatements + ProcedureStatements;
        ProgramStatements.Rule = MakeStarRule(ProgramStatements, NewLinePlus, ProgramStatement);
        gVarStatements.Rule = MakeStarRule(gVarStatements, NewLinePlus, gVarStatement);
        ProcedureStatements.Rule = MakeStarRule(ProcedureStatements, NewLinePlus, ProcedureStatement);
        //'gVar Statement
        gVarStatement.Rule = ToTerm(KeyWords(0)) + Identifier;
        //'Program Statement
        ProgramStatement.Rule = ToTerm(KeyWords(3)) + ProgKeyWord + String;
        ProgKeyWord.Rule = ToTerm(KeyWords(4)) | KeyWords(5) | KeyWords(6);
        //'Procedure Statement
        ProcedureStatement.Rule = ToTerm(KeyWords(1)) + Identifier + KeyWords(7) + ArgStatements + KeyWords(8) + NewLinePlus + Stmnts + NewLinePlus + KeyWords(2);
        ArgStatements.Rule = MakeStarRule(ArgStatements, ToTerm(KeyWords(9)), Identifier);
        Stmnts.Rule = MakeStarRule(Stmnts, NewLinePlus, Stmnt);
        Stmnt.Rule = Identifier + Values;
        Values.Rule = MakeStarRule(Values, Value);
        Value.Rule = String | Number | Identifier;
        this.Root = Program;
//Program :== ProgramStatements gVarStatements ProcedureStatements
//ProgramStatements :== [ProgramStatement+]
//gVarStatements :== [gVarStatement+]
//ProcedureStatements :== [ProcedureStatement+]
//gVarStatement :== "Var" Identifier
//Identifier :== <Base Defination>
//ProgramStatement :== "Program" ProgKw String
//ProgKw :== "Name" Or "Ref" Or "Desc"
//String :== <Base Defination>
//ProcedureStatement :== "Proc" Identifier "[" ArgStatements "]" NewLine+ Stmnts NewLine+ "eProc"
//NewLine :== <Base Defination>
//ArgStatements :== [ArgStatement+]
//ArgStatement :== Identifier
//Stmnts :== [Stmnt+]
//Stmnt :== Identifier Values 
//Values :== [Value+] 
//Value :== String Or Number Or Identifier
//Number :== <Base Defination>

//Program Name "my app"
//Program Ref "abc.dll"
//Program Desc "This is testing program"
//Var Name
//Var Name2
//Proc Name[arg1,arg2]
//   do a ,"b" ,4
//   test "cmd"
//   perform arg1
//   set arg2
I get Parser error Shift-reduce conflicts on inputs: LF

Can anyone help me!
May 8, 2014 at 5:54 PM
you have to get familiar with LALR parsing concepts and what conflicts are, no easy way here. Just google, there's plenty on the subject there. For deeper understanding, get books on compiler construction; like Dragon book, or Parsing Techniques (my favorite)
May 12, 2014 at 7:47 PM
Roman is correct... it really helps to understand parsing concepts to debug & develop your Irony grammars.

I just wanted to give you an additional hint to where your problem is possibly located.

LF = Line-Feed

Looking quickly at your grammar, I see that you use NewLinePlus (which is sometimes referred to as LF+) in your MakeStarRule methods as the delimiter. Try making these part of your statements' rules instead of part of the MakeStarRule. You may still have some further investigation to ensure that multiple rules do not apply for each possible path (i.e. ProcedureStatement).

I have personally found it easier to develop my grammars in the simplest rule definitions as possible and then optimize once I have it working.