Question

I am just starting to use TreeTop to do parsing works. The following is the snippets that puzzles me:

grammar Fortran
    rule integer
        [1-9] [0-9]*
    end

    rule id
        [a-zA-Z] [a-zA-Z0-9]*
    end
end

parser = FortranParser.new
ast = parser.parse('1')

The result ast is:

[SyntaxNode offset=0, "1", SyntaxNode offset=1, ""]

But when I place rule id above rule integer, the result is nil. So what is the problem? Thanks in advance!

Was it helpful?

Solution

I think I just figured out where is wrong!!! There should be a top rule that includes other rules, which is placed as the first rule:

grammar Fortran
    rule statement
        ( id / integer )* {
            def content
                elements.map { |e| e.content }
            end
        }
    end

    rule id
        [a-zA-Z] [a-zA-Z0-9]* {
            def content
                [:id, text_value]
            end
        }
    end

    rule integer
        [1-9] [0-9]* {
            def content
                [:integer, text_value]
            end
        }
    end
end

parser = FortranParser.new
ast = parser.parse('1')

Then the result is

[[:integer, "1"]]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top