parser-combinators

FParsec identifiers vs keywords

大城市里の小女人 提交于 2019-12-01 05:36:03
问题 For languages with keywords, some special trickery needs to happen to prevent for example "if" from being interpreted as an identifier and "ifSomeVariableName" from becoming keyword "if" followed by identifier "SomeVariableName" in the token stream. For recursive descent and Lex/Yacc, I've simply taken the approach (as per helpful instruction) of transforming the token stream between the lexer and the parser. However, FParsec doesn't really seem do a separate lexer step, so I'm wondering what

Combining lexer and parser in a parser combinator

柔情痞子 提交于 2019-12-01 05:27:38
问题 I'm using uu-parsinglib , but I think the following question is parser combinator generic. Let's consider the following example: I've got a lexer with a combinator pLex , which produces a list of tokens (of type MyToken ). I now want to write a parser, which will consume the tokens and build an AST . What is the best way to connect the lexer and parser? Right now I have a lex function: lex s = parse ( (,) <$> pLex <*> pEnd) (createStr (LineColPos 0 0 0) s) Should I create a function parse p =

Scala Parser Token Delimiter Problem

本秂侑毒 提交于 2019-11-30 19:20:34
I'm trying to define a grammar for the commands below. object ParserWorkshop { def main(args: Array[String]) = { ChoiceParser("todo link todo to database") ChoiceParser("todo link todo to database deadline: next tuesday context: app.model") } } The second command should be tokenized as: action = todo message = link todo to database properties = [deadline: next tuesday, context: app.model] When I run this input on the grammar defined below, I receive the following error message: [1.27] parsed: Command(todo,link todo to database,List()) [1.36] failure: string matching regex `\z' expected but `:'

How can I create a parser combinator in which line endings are significant?

早过忘川 提交于 2019-11-30 08:38:55
I am creating a DSL, and using Scala's parser combinator library to parse the DSL. The DSL follows a simple, Ruby-like syntax. A source file can contain a series of blocks that look like this: create_model do at 0,0,0 end Line endings are significant in the DSL, as they are effectively used as statement terminators. I wrote a Scala parser that looks like this: class ML3D extends JavaTokenParsers { override val whiteSpace = """[ \t]+""".r def model: Parser[Any] = commandList def commandList: Parser[Any] = rep(commandBlock) def commandBlock: Parser[Any] = command~"do"~eol~statementList~"end" def

Scala parser combinators vs ANTLR/Java generated parser?

人盡茶涼 提交于 2019-11-30 03:00:58
I am writing an expression parser for an app written mostly in Scala. I have built AST objects in Scala, and now need to write the parser. I have heard of Scala's built-in parser combinators, and also of ANTLR3, and am wondering: which would provide better performance and ease of writing code? So far: ANTLR pros Well-known Fast External DSL ANTLRWorks (great IDE for parser grammer debugging/testing) ANTLR cons Java-based (Scala interop may be challenging, any experience?) Requires a large dependency at runtime Parser combinator pros Part of Scala One less build step No need for a runtime

Scala Parser Token Delimiter Problem

大城市里の小女人 提交于 2019-11-30 02:54:50
问题 I'm trying to define a grammar for the commands below. object ParserWorkshop { def main(args: Array[String]) = { ChoiceParser("todo link todo to database") ChoiceParser("todo link todo to database deadline: next tuesday context: app.model") } } The second command should be tokenized as: action = todo message = link todo to database properties = [deadline: next tuesday, context: app.model] When I run this input on the grammar defined below, I receive the following error message: [1.27] parsed:

Can parser combinators be made efficient?

隐身守侯 提交于 2019-11-29 19:06:15
Around 6 years ago, I benchmarked my own parser combinators in OCaml and found that they were ~5× slower than the parser generators on offer at the time. I recently revisited this subject and benchmarked Haskell's Parsec vs a simple hand-rolled precedence climbing parser written in F# and was surprised to find the F# to be 25× faster than the Haskell. Here's the Haskell code I used to read a large mathematical expression from file, parse and evaluate it: import Control.Applicative import Text.Parsec hiding ((<|>)) expr = chainl1 term ((+) <$ char '+' <|> (-) <$ char '-') term = chainl1 fact ((

How can I create a parser combinator in which line endings are significant?

青春壹個敷衍的年華 提交于 2019-11-29 12:11:12
问题 I am creating a DSL, and using Scala's parser combinator library to parse the DSL. The DSL follows a simple, Ruby-like syntax. A source file can contain a series of blocks that look like this: create_model do at 0,0,0 end Line endings are significant in the DSL, as they are effectively used as statement terminators. I wrote a Scala parser that looks like this: class ML3D extends JavaTokenParsers { override val whiteSpace = """[ \t]+""".r def model: Parser[Any] = commandList def commandList:

Scala parser combinators vs ANTLR/Java generated parser?

谁说胖子不能爱 提交于 2019-11-29 00:37:55
问题 I am writing an expression parser for an app written mostly in Scala. I have built AST objects in Scala, and now need to write the parser. I have heard of Scala's built-in parser combinators, and also of ANTLR3, and am wondering: which would provide better performance and ease of writing code? So far: ANTLR pros Well-known Fast External DSL ANTLRWorks (great IDE for parser grammer debugging/testing) ANTLR cons Java-based (Scala interop may be challenging, any experience?) Requires a large

Understanding the tilde in Scala's parser combinators

随声附和 提交于 2019-11-28 20:26:56
I'm fairly new to Scala and while reading about parser combinators( The Magic Behind Parser Combinators , Domain-Specific Languages in Scala ) I came across method definitions like this: def classPrefix = "class" ~ ID ~ "(" ~ formals ~ ")" I've been reading throught the API doc of scala.util.parsing.Parsers which defines a method named (tilde) but I still dont't really understand its usage in the example above. In that example (tilde) is a method which is called on java.lang.String which doesn't have that method and causes the compiler to fail. I know that (tilde) is defined as case class ~ [