Re: Parser combinators in parsatron
On Thu, Aug 23, 2012 at 11:22 AM, Alexsandro Soares prof.asoa...@gmail.com wrote: Can you provide the code for this? Certainly. The parser's current source position is stored in the InputState record's `pos` field. That field is a SourcePos record consisting of the current line and column position. Most parsers, however don't deal directly with the raw InputState and SourcePositions, so we must write a low-level function to examine those records, and return the value we're interested in without consuming any input. The function would look like this: (defn lineno [] (fn [state _ _ eok _] (bounce eok (:line (:pos state)) state))) The first line defines the name of the fn we can use in let- statements to get the current line number. The second line is the standard parser interface, a function that takes the state and 4 continutation functions, of which we're only interested in the one that doesn't consume any input and doesn't produce an error. The last line pulls the current line number from the state, and passes it along with the unchanged state to the `eok` continuation (`bounce` is the function we use to ensure that no stack is consumed in the process). Now, you can treat `lineno` just like any other parser (in truth, it is very close in spirit to the `always` parser except that it operates on the parsers current state). (let- [current-line (lineno)] (do-something-with current-line)) Hope this was helpful. Nate -- You received this message because you are subscribed to the Google Groups Clojure group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en
Re: Parser combinators in parsatron
Armando, Nate and Panduranga Many thanks for the answers. Regards, Alex 2012/8/28 Nate Young youn...@gmail.com On Thu, Aug 23, 2012 at 11:22 AM, Alexsandro Soares prof.asoa...@gmail.com wrote: Can you provide the code for this? Certainly. The parser's current source position is stored in the InputState record's `pos` field. That field is a SourcePos record consisting of the current line and column position. Most parsers, however don't deal directly with the raw InputState and SourcePositions, so we must write a low-level function to examine those records, and return the value we're interested in without consuming any input. The function would look like this: (defn lineno [] (fn [state _ _ eok _] (bounce eok (:line (:pos state)) state))) The first line defines the name of the fn we can use in let- statements to get the current line number. The second line is the standard parser interface, a function that takes the state and 4 continutation functions, of which we're only interested in the one that doesn't consume any input and doesn't produce an error. The last line pulls the current line number from the state, and passes it along with the unchanged state to the `eok` continuation (`bounce` is the function we use to ensure that no stack is consumed in the process). Now, you can treat `lineno` just like any other parser (in truth, it is very close in spirit to the `always` parser except that it operates on the parsers current state). (let- [current-line (lineno)] (do-something-with current-line)) Hope this was helpful. Nate -- You received this message because you are subscribed to the Google Groups Clojure group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -- Alexsandro Santos Soares, Prof. Dr. Faculdade de Computação Universidade Federal de Uberlândia - UFU - Sala 1B123 +55 (0xx34) 3239-4478 -- You received this message because you are subscribed to the Google Groups Clojure group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en
Re: Parser combinators in parsatron
Alexsandro, I don't know about Parsatron, but Parse-EZ ( https://github.com/protoflex/parse-ez) provides the 'line-pos function that returns [line# column#] vector. Here is the equivalent code for your example using Parse-EZ: (use 'protoflex.parse) (defn anbn [] (let [as (regex #a+) bs (string (apply str (repeat (count as) \b)))] (str as bs))) (defn xdny [] (regex #xd+y)) (defn pL [] (any anbn xdny)) You can call your parse function pL through the parser entry function parse: user= (parse pL xddy) xddy user= (parse pL aabb) aabb user= (parse pL aacc) Parse Error (exception thrown) Panduranga Adusumilli -- You received this message because you are subscribed to the Google Groups Clojure group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en
Re: Parser combinators in parsatron
Alexsandro, I don't know about Parsatron, but Parse-EZ ( https://github.com/protoflex/parse-ez) provides the 'line-pos' function that returns the line and column info. Here is the equivalent code for your example using Parse-EZ: -- (use 'protoflex.parse) (defn anbn [] (let [as (regex #a+) bs (string (apply str (repeat (count as) \b)))] (str as bs))) (defn xdny [] (regex #xd+y)) (defn pL [] (any anbn xdny)) - and you call your pL function through the parser entry function parse: user= (parse pL xddy) xddy user= (parse pL aabb) aabb user= (parse pL aac) Parse Error (exception thrown) On Thursday, August 23, 2012 7:54:16 PM UTC+5:30, Alexsandro Soares wrote: Ok. Thanks for the answer. Is there any way to get the line and column? For example, in this parser (defparser ident [] ( (letter) (many (either (letter) (digit) I want the token and the initial line and column. How can I change this code? Cheers, Alex -- You received this message because you are subscribed to the Google Groups Clojure group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en
Re: Parser combinators in parsatron
Ok. Thanks for the answer. Is there any way to get the line and column? For example, in this parser (defparser ident [] ( (letter) (many (either (letter) (digit) I want the token and the initial line and column. How can I change this code? Cheers, Alex -- You received this message because you are subscribed to the Google Groups Clojure group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en
Re: Parser combinators in parsatron
On Thu, Aug 23, 2012 at 9:24 AM, Alexsandro Soares prof.asoa...@gmail.com wrote: Ok. Thanks for the answer. Is there any way to get the line and column? The Parsatron doesn't have any builtin facilities for extracting line numbers from tokens, you'd have to keep track of the number of newline characters your parser has seen through some manually-implemented manner. Nate -- You received this message because you are subscribed to the Google Groups Clojure group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en
Re: Parser combinators in parsatron
Hi Nate, Can you provide the code for this? Thanks, Alex 2012/8/23 Nate Young youn...@gmail.com On Thu, Aug 23, 2012 at 9:24 AM, Alexsandro Soares prof.asoa...@gmail.com wrote: Ok. Thanks for the answer. Is there any way to get the line and column? The Parsatron doesn't have any builtin facilities for extracting line numbers from tokens, you'd have to keep track of the number of newline characters your parser has seen through some manually-implemented manner. Nate -- You received this message because you are subscribed to the Google Groups Clojure group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en
Parser combinators in parsatron
Hi all, I'm using the Parsatron library to build parser combinators. I have the following definition: (defparser anbn [] (let- [as (many (char \a)) bs (times (count as) (char \b))] (always (concat as bs (defparser xdny [] (let- [ds (between (char \x) (char \y) (many (char \d)))] (always (concat '(\x) ds '(\y) (defparser pL [] (either (anbn) (xdny))) When I use this definition in REPL with some examples, I have: user= (run (pL) xddy) () user= (run (pL) aabb) (\a \a \b \b) user= (run (xdny) xddy) (\x \d \d \y) I didn't understand why the first answer is not (\x \d \d \y). The second and third answers are correct for me. Can anyone help me? Regards, Alex -- You received this message because you are subscribed to the Google Groups Clojure group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en
Re: Parser combinators in parsatron
pL first tries anbn: many parses zero \a's; then times has to parse zero \b's; and the parser returns the concatenation of two empty lists. An empty list isn't a failure as far as the parser either is concerned, so it won't try xdny in that case. On Wednesday, August 22, 2012 5:38:56 PM UTC-7, Alexsandro Soares wrote: Hi all, I'm using the Parsatron library to build parser combinators. I have the following definition: (defparser anbn [] (let- [as (many (char \a)) bs (times (count as) (char \b))] (always (concat as bs (defparser xdny [] (let- [ds (between (char \x) (char \y) (many (char \d)))] (always (concat '(\x) ds '(\y) (defparser pL [] (either (anbn) (xdny))) When I use this definition in REPL with some examples, I have: user= (run (pL) xddy) () user= (run (pL) aabb) (\a \a \b \b) user= (run (xdny) xddy) (\x \d \d \y) I didn't understand why the first answer is not (\x \d \d \y). The second and third answers are correct for me. Can anyone help me? Regards, Alex -- You received this message because you are subscribed to the Google Groups Clojure group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en