Fsyacc:已添加具有相同密钥的项目

发布于 2024-11-03 07:49:36 字数 2096 浏览 5 评论 0原文

我开始玩 Fslex/Fsyacc。尝试使用此输入生成解析器时

Parser.fsy:

%{
open Ast
%}

// The start token becomes a parser function in the compiled code:
%start start

// These are the terminal tokens of the grammar along with the types of
// the data carried by each token:
%token <System.Int32> INT
%token <System.String> STRING
%token <System.String> ID
%token PLUS MINUS ASTER SLASH LT LT EQ GTE GT
%token LPAREN RPAREN LCURLY RCURLY LBRACKET RBRACKET COMMA
%token ARRAY IF THEN ELSE WHILE FOR TO DO LET IN END OF BREAK NIL FUNCTION VAR TYPE IMPORT PRIMITIVE
%token EOF

// This is the type of the data produced by a successful reduction of the 'start'
// symbol:
%type <Ast.Program> start

%%

// These are the rules of the grammar along with the F# code of the 
// actions executed as rules are reduced.  In this case the actions 
// produce data using F# data construction terms.
start: Prog { Program($1) }

Prog:
    | Expr EOF                  { $1 }

Expr: 
    // literals
    | NIL                   { Ast.Nil ($1) }
      | INT                       { Ast.Integer($1) }
    | STRING                    { Ast.Str($1) }

    // arrays and records
    | ID LBRACKET Expr RBRACKET OF Expr { Ast.Array ($1, $3, $6) } 
    | ID LCURLY AssignmentList RCURLY { Ast.Record ($1, $3) }

AssignmentList:
    | Assignment { [$1] }
    | Assignment COMMA AssignmentList {$1 :: $3 }

Assignment:
    | ID EQ Expr { Ast.Assignment ($1,$3) }

Ast.fs

namespace Ast
open System

type Integer = 
    | Integer of Int32

and Str = 
    | Str of string

and Nil = 
    | None

and Id = 
    | Id of string

and Array = 
    | Array of Id * Expr * Expr

and Record = 
    | Record of Id * (Assignment list)

and Assignment = 
    | Assignment of Id * Expr

and Expr =
    | Nil
    | Integer
    | Str
    | Array
    | Record

and Program =
    | Program of Expr

Fsyacc 报告以下错误:“FSYACC:错误 FSY000:具有相同密钥的项目已已添加。”

我相信问题出在 AssignmentList 的生产中,但找不到解决方法...

任何提示将不胜感激

I'm starting to play with Fslex/Fsyacc. When trying to generate the parser using this input

Parser.fsy:

%{
open Ast
%}

// The start token becomes a parser function in the compiled code:
%start start

// These are the terminal tokens of the grammar along with the types of
// the data carried by each token:
%token <System.Int32> INT
%token <System.String> STRING
%token <System.String> ID
%token PLUS MINUS ASTER SLASH LT LT EQ GTE GT
%token LPAREN RPAREN LCURLY RCURLY LBRACKET RBRACKET COMMA
%token ARRAY IF THEN ELSE WHILE FOR TO DO LET IN END OF BREAK NIL FUNCTION VAR TYPE IMPORT PRIMITIVE
%token EOF

// This is the type of the data produced by a successful reduction of the 'start'
// symbol:
%type <Ast.Program> start

%%

// These are the rules of the grammar along with the F# code of the 
// actions executed as rules are reduced.  In this case the actions 
// produce data using F# data construction terms.
start: Prog { Program($1) }

Prog:
    | Expr EOF                  { $1 }

Expr: 
    // literals
    | NIL                   { Ast.Nil ($1) }
      | INT                       { Ast.Integer($1) }
    | STRING                    { Ast.Str($1) }

    // arrays and records
    | ID LBRACKET Expr RBRACKET OF Expr { Ast.Array ($1, $3, $6) } 
    | ID LCURLY AssignmentList RCURLY { Ast.Record ($1, $3) }

AssignmentList:
    | Assignment { [$1] }
    | Assignment COMMA AssignmentList {$1 :: $3 }

Assignment:
    | ID EQ Expr { Ast.Assignment ($1,$3) }

Ast.fs

namespace Ast
open System

type Integer = 
    | Integer of Int32

and Str = 
    | Str of string

and Nil = 
    | None

and Id = 
    | Id of string

and Array = 
    | Array of Id * Expr * Expr

and Record = 
    | Record of Id * (Assignment list)

and Assignment = 
    | Assignment of Id * Expr

and Expr =
    | Nil
    | Integer
    | Str
    | Array
    | Record

and Program =
    | Program of Expr

Fsyacc reports the following error: "FSYACC: error FSY000: An item with the same key has already been added."

I believe the problem is in the production for AssignmentList, but can't find a way around...

Any tips will be appreciated

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

地狱即天堂 2024-11-10 07:49:36

讨厌回答我自己的问题,但问题就在这里(解析器输入文件的第 15 行)

%token PLUS MINUS ASTER SLASH LT LT EQ GTE GT

注意双重定义(应该是 LTE)

我的投票是寻找改进 Fslex/Fsyacc 可执行文件/msbuild 任务输出的方法

Hate answering my own questions, but the problem was here (line 15 of parser input file)

%token PLUS MINUS ASTER SLASH LT LT EQ GTE GT

Note the double definition (should have been LTE)

My vote goes for finding a way to improve the output of the Fslex/Fsyacc executables/msbuild tasks

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文