FsLexYacc compile errors?

129 views Asked by At

I'm trying to setup a new project in F#.

I'm using FsLexYacc as a tool, and last time i used it was when the Fsharp powerpack was 'in'. The documentation on the site are not that good. It also seems to me that there is a bug with the generic type annotation 'end since it is a keyword..

but I'm in first place just copy pasting the dummi files from the page to make sure the makefile are up and running. (lexer, parser, and program)

page: https://fsprojects.github.io/FsLexYacc/index.html

I then get the

../../FSharp/Project/src/Lexer.fsl(21,81): error FS0001: The type 'char' does not match the type 'byte'

Have tried to enforce the type by changing _ to byte that didn't help at all

Makefile:

OS=$(shell uname -s)
ifeq ($(OS),Darwin)
  export AS=as -arch i386
  export CC=cc -arch i386 -framework CoreFoundation -lobjc -liconv
endif

.PHONY: all clean

fsl = fslex
fsp = fsyacc
fsc = fsharpc --nologo

fsyacclib = FsLexYacc.10.2.0/build/fsyacc/netcoreapp3.1/FsLexYacc.Runtime.dll

Main = bin/Main.exe

LexerGen    = src/Lexer.fs
ParserGen   = src/Parser.fs

Lexer       = bin/Lexer.dll
Parser      = bin/Parser.dll    


all: $(Main)

$(LexerGen): src/Lexer.fsl 
    $(fsl) src/Lexer.fsl -o $(LexerGen)

$(ParserGen): src/Parser.fsy
    $(fsp) -v --module Parser src/Parser.fsy -o $(ParserGen)

$(Lexer):  $(LexerGen) $(Parser) 
    $(fsc) -a $(LexerGen) -r $(Parser) -o $(Lexer)

$(Parser): $(ParserGen) $(Regex) $(fsyacclib)
    $(fsc) -a $(ParserGen) -r $(Regex) -r $(fsyacclib) -o $(Parser) 

$(Main): src/Main.fsx $(Lexer) $(Parser) 
    $(fsc) -a src/Main.fsx -r $(fsyacclib)  -r $(Lexer) -r $(Parser) -o $(Main)


clean: rm /bin/*.dll 
1

There are 1 answers

2
Tomas Petricek On

I tried this, but I was not able to reproduce your error. Here is what I'm doing. Is there something I'm doing differently than you?

I copied the Lexer.fsl file from the repository:

{

// Opens methods related to fslex.exe
open FSharp.Text.Lexing

let newline (lexbuf: LexBuffer<_>) = 
  lexbuf.StartPos <- lexbuf.StartPos.NextLine

}

// Regular expressions
let whitespace = [' ' '\t' ]
let newline = ('\n' | '\r' '\n')

rule tokenstream = parse
// --------------------------
| "hello"       { Parser.HELLO }
// --------------------------
| whitespace    { tokenstream lexbuf }
| newline   { newline lexbuf; tokenstream lexbuf }
// --------------------------
| _         { failwith ("ParseError" + LexBuffer<_>.LexemeString lexbuf) }
| eof       { Parser.EOF }

And the Parser.fsy file:

%{


%}

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

// Regular tokens
%token HELLO

// Misc tokens
%token EOF

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

%%

// These are the rules of the grammar along with the F# code of the 
// actions executed as rules are reduced.  
start: File end { $1 }
     | end      { $1 }

File:
    | HELLO                     { 1 }
    | HELLO HELLO               { 2 }


// Using F# keywords for nonterminal names is okay.
end: EOF { 3 }

And run the following command to compile the two:

fsc Parser.fs Lexer.fs -r C:\[my work folder]\FsLexYacc\src\FsLexYacc.Runtime\bin\Debug\netstandard2.0\FsLexYacc.Runtime.dll

The source code in the generated Lexer.fs looks something like this:

module Lexer
# 1 "Lexer.fsl"
 

// Opens methods related to fslex.exe
open FSharp.Text.Lexing

let newline (lexbuf: LexBuffer<_>) = 
  lexbuf.StartPos <- lexbuf.StartPos.NextLine


# 12 "Lexer.fs"
let trans : uint16[] array = 
    [| 
    (* State 0 *)
     [| 5us;5us;5us;5us; (* lots more here... *)|];
    (* lots more states here... *)
    (* State 11 *)
     [| 65535us;65535us;65535us; (* lots more here... *)|];
    |] 
let actions : uint16[] = [|65535us;3us;1us;2us;3us;3us;4us;2us;65535us;65535us;65535us;0us;|]
let _fslex_tables = FSharp.Text.Lexing.UnicodeTables.Create(trans,actions)
let rec _fslex_dummy () = _fslex_dummy() 
// Rule tokenstream
and tokenstream  lexbuf =
  match _fslex_tables.Interpret(0,lexbuf) with
  | 0 -> ( 
# 17 "Lexer.fsl"
                             Parser.HELLO 
# 49 "Lexer.fs"
          )
  | 1 -> ( 
# 19 "Lexer.fsl"
                              tokenstream lexbuf 
# 54 "Lexer.fs"
          )
  | 2 -> ( 
# 20 "Lexer.fsl"
                           newline lexbuf; tokenstream lexbuf 
# 59 "Lexer.fs"
          )
  | 3 -> ( 
# 22 "Lexer.fsl"
                          failwith ("ParseError" + LexBuffer<_>.LexemeString lexbuf) 
# 64 "Lexer.fs"
          )
  | 4 -> ( 
# 23 "Lexer.fsl"
                          Parser.EOF 
# 69 "Lexer.fs"
          )
  | _ -> failwith "tokenstream"

# 3000000 "Lexer.fs"