happy

Haskell Happy parser error mismatching types and infinite type

给你一囗甜甜゛ 提交于 2020-01-17 07:20:49
问题 Writing a Oberon-like language parser, I'm having troubles compiling the parser after I've updated it to be able to define more procedures on the same level, and not only nested one inside the other. This is my lexer: { module Lexer where } %wrapper "basic" $alpha = [a-zA-Z] $digit = [0-9] $validChar = [^\"] tokens :- $white+ ; "PROCEDURE" { \s -> KW_TokenProcedure } "END" { \s -> KW_TokenEnd } ";" { \s -> KW_TokenSemiColon } $alpha [$alpha $digit \_]* { \s -> TokenVariableIdentifier s } { --

Resolve conflict in bison grammar with space separated expression lists + if/then/else

对着背影说爱祢 提交于 2019-12-29 09:34:47
问题 I have the following yacc/bison/happy grammar: %token if TokenIf then TokenThen else TokenElse true TokenTrue false TokenFalse %left APP %right IF %% Hungry : NoHungry | Hungry NoHungry %prec APP | if Hungry then Hungry else Hungry %prec IF NoHungry : true | false bison -v tells me there are two conflicts in the following situation: State 12 2 Hungry: Hungry . NoHungry 3 | if Hungry then Hungry else Hungry . true shift, and go to state 2 false shift, and go to state 3 true [reduce using rule

No instance for (Show ([(String, Int)] -> Int))

烈酒焚心 提交于 2019-12-25 04:57:09
问题 to calculate the value of the expression on the fly at the production rules in happy doesn't work if I'm using the lambda expressions. For example this code Exp : let var '=' Exp in Exp { \p -> $6 (($2,$4 p):p) } | Exp1 { $1 } Exp1 : Exp1 '+' Term { \p -> $1 p + $3 p } | Exp1 '-' Term { \p -> $1 p - $3 p } | Term { $1 } Term : Term '*' Factor { \p -> $1 p * $3 p } | Term '/' Factor { \p -> $1 p `div` $3 p } | Factor { $1 } Factor : int { \p -> $1 } | var { \p -> case lookup $1 p of Nothing ->

GLR_Lib.hs: Could not find module 'System'

烂漫一生 提交于 2019-12-25 02:53:40
问题 I am trying to generate a GLR parser from happy, but I am getting errors once the files are generated. Here is an example, ABC.y , so it's clear what I am trying: { module Main where } %name ps1 s1 %tokentype { ABC } %error { parseError } %token a { A } b { B } c { C } %% s1: a a a b {} | b s2 a {} s2: b a b s2 {} | c {} { data ABC = A | B | C parseError _ = error "bad" main = getContents >>= print . ps1 . lexer lexer ('a':xs) = A : lexer xs ETC } This example works just fine with happy ABC.y

Happy Context-Dependent Operator Precedence

↘锁芯ラ 提交于 2019-12-22 05:33:35
问题 I have two snippets of Happy code here, one that uses normal precedence rules, and one that uses context-dependent precedence rules (both of which are described here). Normal: %left '+' %left '*' %% Exp :: { Exp } : Exp '+' Exp { Plus $1 $3 } | Exp '*' Exp { Times $1 $3 } | var { Var $1 } Context-dependent: %left PLUS %left TIMES %% Exp :: { Exp } : Exp '+' Exp %prec PLUS { Plus $1 $3 } | Exp '*' Exp %prec TIMES { Times $1 $3 } | var { Var $1 } Given the input: a * b + c * d The normal

Using alex/happy with Cabal

£可爱£侵袭症+ 提交于 2019-12-21 07:55:14
问题 I'm writing a compiler for a class I'm taking. The class isn't specifically Haskell but I'm using Haskell to write my compiler and interpreter. I have a cabal package setup to hopefully make it easy for my prof to run/compile. I have happy and alex in the build-tools field for both executables but Cabal ignores that and then complains that it cannot find the modules that Happy and Alex should be generating. If I manually run: alex LimpScanner.x happy LimpParser.y then cabal runs perfectly. I

How to Lex, Parse, and Serialize-to-XML Email Messages using Alex and Happy

回眸只為那壹抹淺笑 提交于 2019-12-12 17:17:59
问题 I am working toward being able to input any email message and output an equivalent XML encoding. I am starting small, with one of the email headers -- the "From Header" Here is an example of a From Header: From: John Doe <john@doe.org> I want it transformed into this XML: <From> <Mailbox> <DisplayName>John Doe</DisplayName> <Address>john@doe.org</Address> </Mailbox> </From> I want to use the lexical analyzer "Alex" (http://www.haskell.org/alex/doc/html/) to break apart (tokenize) the From

Show custom errors while parsing Happy Haskell

五迷三道 提交于 2019-12-12 15:13:24
问题 I'm writing a monadic parser using Alex and Happy in Haskell. My error function is defined like this: parseError :: Token -> Alex a parseError _ = alexError "error occurred" How can I send custom errors (like incorrect type while trying to add a string to a number) during parsing? UPDATE The parser doesn't need to do the type checking, I'm doing it inside the production since I keep track of the operands type. As said in a comment, I cannot use the parseError , so is there a way to print an

Parsing with parenthesis and different types of expressions

安稳与你 提交于 2019-12-12 06:37:00
问题 I'm currently using happy to parse a language, but I don't think the parser is relevant, except to say it's an LALR parser. Here's a small excerpt from the grammar: ArithExpr -> ArithExpr + ArithExpr ArithExpr -> ( ArithExpr ) ArithExpr -> ... BoolExpr -> ArithExpr == ArithExpr BoolExpr -> ( BoolExpr ) BoolExpr -> ... The problem is that I'm getting reduce-reduce conflicts. I think the issue arises when I try to parse something like the following: ( ( 2 + 3 ) == ( 4 + 5 ) ) There's only one

What causes Happy to throw a parse error?

假如想象 提交于 2019-12-10 11:04:18
问题 I've written a lexer in Alex and I'm trying to hook it up to a parser written in Happy. I'll try my best to summarize my problem without pasting huge chunks of code. I know from my unit tests of my lexer that the string "\x7" is lexed to: [TokenNonPrint '\x7', TokenEOF] My token type (spit out by the lexer), is Token . I've defined lexWrap and alexEOF as described here, which gives me the following header and token declarations: %name parseTokens %tokentype { Token } %lexer { lexWrap } {