首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么这个fsyacc输入生成不能编译的F#?

为什么这个fsyacc输入生成不能编译的F#?
EN

Stack Overflow用户
提问于 2011-11-04 01:09:15
回答 1查看 740关注 0票数 2

我的fsyacc代码给出了一个编译器错误,指出找不到一个变量,但我不确定原因。我希望有人能指出这个问题。

代码语言:javascript
复制
%{
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 NAME
%token ARROW TICK VOID
%token LPAREN RPAREN
%token EOF

// This is the type of the data produced by a successful reduction of the 'start'
// symbol:
%type < Query > 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: Query { Terms($1) }

Query:
    | Term EOF                  { $1 }

Term: 
    | VOID                      { Void }
    | NAME                      { Conc($1) }
    | TICK NAME                 { Abst($2) }
    | LPAREN Term RPAREN        { Lmda($2) }
    | Term ARROW Term           { TermList($1, $3) }

行| NAME {Conc($1)}和以下行都显示此错误:

代码语言:javascript
复制
  error FS0039: The value or constructor '_1' is not defined

我理解语法问题,但是yacc输入有什么问题呢?

如果有帮助,下面是Ast的定义:

代码语言:javascript
复制
namespace Ast
open System

type Query =
    | Terms   of Term

and Term =
    | Void
    | Conc of String
    | Abst of String
    | Lmda of Term
    | TermList of Term * Term

和fslex输入:

代码语言:javascript
复制
{
module Lexer
open System
open Parser
open Microsoft.FSharp.Text.Lexing

let lexeme lexbuf =
    LexBuffer<char>.LexemeString lexbuf
}

// These are some regular expression definitions
let name = ['a'-'z' 'A'-'Z' '0'-'9']
let whitespace = [' ' '\t' ]
let newline = ('\n' | '\r' '\n')

rule tokenize = parse
| whitespace    { tokenize lexbuf }
| newline       { tokenize lexbuf }
// Operators
| "->"          { ARROW }
| "'"           { TICK }
| "void"        { VOID }
// Misc
| "("           { LPAREN }
| ")"           { RPAREN }
// Numberic constants
| name+                                 { NAME }
// EOF
| eof   { EOF }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-11-04 01:24:25

这不是FsYacc的错。NAME是一个没有价值的令牌。

你会想要做这些修复:

%token NAME%token <string> NAME

| name+ { NAME }| name+ { NAME (lexeme lexbuf) }

现在一切都应该被编译了。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7999019

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档