0

I'm currently using the alex and happy lexer/parser generators to implement a parser for the Ethereum Smart contract language solidity. Currently I'm using a reduced grammar in order to simplify the initial development.

I'm running into an error parsing the 'contract' section of the my test contract file.

The following is the code for the grammar:

ProgSource   :: { ProgSource }
ProgSource   : SourceUnit                  { ProgSource $1 }

SourceUnit   : PragmaDirective             { SourceUnit $1}

PragmaDirective : "pragma" ident ";"       {Pragma $2 }
                | {- empty -}              { [] }

ImportDirective : 
                "import" stringLiteral ";"  { ImportDir $2 }

ContractDefinition : contract ident "{" ContractPart "}"  { Contract $2 $3 }

ContractPart : StateVarDecl                   { ContractPart $1 }

StateVarDecl : TypeName "public" ident ";"    { StateVar $1 $3 }
                         | TypeName "public" ident "=" Expression ";"       { StateV $1 $3 $5 }

The following file is my test 'contract':

pragma solidity;
contract identifier12 {
   public variable = 1;
}

The result is from passing in my test contract into the main function of my parser.

$ cat test.txt | ./main
main: Parse error at TContract (AlexPn 17 2 1)2:1
CallStack (from HasCallStack):
    error, called at ./Parser.hs:232:3 in main:Parser

From the error it suggest that the issue is the first letter of the 'contract' token, on line 2 column 1. But from my understanding this should parse properly?

Sheldor
  • 11
  • 4

1 Answers1

1

You defined ProgSource to be a single SourceUnit, so the parser fails when the second one is encountered. I guess you wanted it to be a list of SourceUnits.

The same applies to ContractPart.

Also, didn't you mean to quote "contract" in ContractDefinition? And in the same production, $3 should be $4.

rici
  • 234,347
  • 28
  • 237
  • 341
  • Thanks for the response, I've managed to get the code working by modifying 'programSource' to be of 0 or many style rule and it seems to be working properly now. – Sheldor Feb 02 '18 at 16:30