Loading ...
Sorry, an error occurred while loading the content.
 

newbiew question on parsar grammar

Expand Messages
  • John MccLain
    Howdy, Sebastian Kaliszewski replied to an earlier post of mine (See original post below) stating that the parser was waiting for more input. He was correct -
    Message 1 of 2 , Dec 10 8:44 AM
      Howdy,
      Sebastian Kaliszewski replied to an earlier post of mine (See original post
      below) stating that the parser was waiting
      for more input. He was correct - If I entered another NUM, the parser
      processed it and continued to wait again. He also mentioned that my
      startrule should have a check for EOF. I.E.:
      Your start rule should have have EOF checking in it.
      something like:

      expression: simpleexp ((LE|GE|NE|EQ|LT|GT) simpleexp )? EOF

      EOF checking is not implicit since many languages allow arbitrary
      garbage afterthe program.

      BUT, what is the rule for EOF, and will this cause the parser to stop busy
      waiting for input? That is what I want to do. Right now I am using standard
      input (through the console in eclipse), but ultimately, I will be passing an
      expression or function
      string into the parser for it to build a parse tree. How would I gracefully
      exit the parser at the end of the input string ( or for now, standard in)


      Here is my original post :


      I am not understanding this at all. I am building a grammar
      to parse powerbuilder functions. Here is all of the parser grammar - I
      reduced it down
      to localize
      the confusion

      expression: simpleexp ((LE|GE|NE|EQ|LT|GT) simpleexp )?
      ;

      simpleexp:term
      ;

      term: factor
      ;

      factor: NUM
      ;
      and the lexer grammar (k = 4) for NUM is:

      NUM: ('0'..'9')+
      {System.out.println("2" + getText());}
      ;

      What I dont understand is the ANTLR grammar syntax (I think).
      Given an input of "1" (without the quotes), The rule 'simpleexp
      ((LE|GE|NE|EQ|LT|GT) simpleexp )?'
      causes the parser to process then hang inside the parser, and not exit.
      However, when I change the rule to
      just 'simpleexp', the parser does not hang, and I exit gracefully out of
      main. The parser start rule is 'expression'.
      My question is - why does 'simpleexp ((LE|GE|NE|EQ|LT|GT) simpleexp )?' not
      work? the parenthetic stmt is OPTIONAL - I wouldn't think it should cause a
      different behavior than just 'simpleexp'. If I use 'simpleexp | simpleexp
      ((LE|GE|NE|EQ|LT|GT) simpleexp )?', all is well, but I don't understand why
      I should have to do this???

      John McClain
      Senior Software Engineer
      TCS Healthcare
      jmcclain@...
      (530)886-1700x235
      "Before you criticize someone, walk a mile in their shoes.
      That way, you'll be a mile from them, and you'll have their shoes."
    • Sebastian Kaliszewski
      ... The rule (token) for EOF is EOF. Exactly as i wrote. ... It should ... Just use that soecial EOF token. It s automatically generated byt the lexer when
      Message 2 of 2 , Dec 12 4:12 AM
        Dnia pią 10. grudzień 2004 17:44, John MccLain napisał:
        > Howdy,
        > Sebastian Kaliszewski replied to an earlier post of mine (See original
        > post below) stating that the parser was waiting
        > for more input. He was correct - If I entered another NUM, the parser
        > processed it and continued to wait again. He also mentioned that my
        > startrule should have a check for EOF. I.E.:
        > Your start rule should have have EOF checking in it.
        > something like:
        >
        > expression: simpleexp ((LE|GE|NE|EQ|LT|GT) simpleexp )? EOF
        >
        > EOF checking is not implicit since many languages allow arbitrary
        > garbage afterthe program.
        >
        > BUT, what is the rule for EOF,

        The rule (token) for EOF is EOF. Exactly as i wrote.

        > and will this cause the parser to stop busy
        > waiting for input?

        It should

        > That is what I want to do. Right now I am using standard
        > input (through the console in eclipse), but ultimately, I will be passing
        > an expression or function
        > string into the parser for it to build a parse tree. How would I gracefully
        > exit the parser at the end of the input string ( or for now, standard in)

        Just use that soecial EOF token. It's automatically generated byt the lexer
        when input terminates.


        rgds
        --
        Sebastian Kaliszewski
      Your message has been successfully submitted and would be delivered to recipients shortly.