Coder Social home page Coder Social logo

How to match EOF? about farkle HOT 4 CLOSED

teo-tsirpanis avatar teo-tsirpanis commented on September 27, 2024
How to match EOF?

from farkle.

Comments (4)

teo-tsirpanis avatar teo-tsirpanis commented on September 27, 2024

Hello and happy new year!

I looked at your code and at the problem's statement. Each passport declaration has to be divided by a blank line (or as you wrote, two consecutive new lines). Instead of adding a "\r\n\r\n" at the end of the KeyValuePairs' production, you can instead use the sepBy operator to separate the passport declarations with a literal of two new lines.

With some additional changes, I took the liberty of refactoring the attached snippet like this:

namespace AdventOfCode.Core

module Day4 =

    open Farkle
    open Farkle.Builder
    open Farkle.Builder.Regex

    type Field =
        | BirthYear
        | IssueYear
        | ExpirationYear
        | Height
        | HairColor
        | EyeColor
        | PassportID
        | CountryID

    type KeyValuePair = { Key : Field; Value : string; }

    type Passport = Passport of KeyValuePair list

    type Parser () =

        static let field =
            // Writing the field type as a nonterminal makes the parser more readable and case-insensitive.
            [
                "byr", BirthYear
                "iyr", IssueYear
                "eyr", ExpirationYear
                "hgt", Height
                "hcl", HairColor
                "ecl", EyeColor
                "pid", PassportID
                "cid", CountryID
            ]
            |> List.map (fun (name, x) -> !& name =% x)
            |> (||=) "Field"

        static let value =
            // The regex was shortened.
            regexString "([a-z]|\d|#)+"
            |> terminal "Value" (T(fun _ data -> data.ToString()))

        static let keyValuePair = "KeyValuePair" ||= [
            !@ field .>> ":" .>>. value => (fun k v -> { Key = k; Value = v })
        ]

        static let passport = "Passport" ||= [
            !@ (many1 keyValuePair) => Passport
        ]

        // This designtime Farkle matches many different passports in the same
        // input file (I assume you were calling the parser many times)
        static let passports = sepBy (literal "\r\n\r\n") passport

        // Declaring the runtime Farkle in a static member will cause it to be rebuilt every
        // time the property is accesed, resulting in a significant waste of computation.
        // I also took the liberty to remove the runtime Farkle for the KeyValuePair
        // (unless I am mistaken it doesn't seem necessary).
        static let runtime = RuntimeFarkle.build passports

        static member internal parse input = 
            match RuntimeFarkle.parseString runtime input with
            | Ok result -> result
            | Error err -> failwith (err.ToString())

I tested this modified parser with the sample input and it worked. I believe it will work for the purposes of the problem.

However, in the general case, it is brittle and doesn't always work. Try adding three new lines between two passports and the parser will view them as one passport. Try adding two new lines at the end of the text and the parser will fail with a syntax error.

The correct way to parse new lines in Farkle is through the newline operator. I tried to rewrite the parser using it but it failed with a Shift-Reduce conflict because Farkle has trouble distinguishing between the optional new lines between passport fields and the at least two new lines that separate passport declarations. But as I said before, it should work in your case.

from farkle.

teo-tsirpanis avatar teo-tsirpanis commented on September 27, 2024

To actually answer how to match EOF (even though it doesn't seem to be necessary in this case), it can be done using virtual terminals and by writing a custom tokenizer. This repository has a well-documented sample that uses virtual terminals and a custom tokenizer. A tokenizer that just matches EOF would be much simpler.

This feature is already implemented and is scheduled to be shipped with Farkle 6.0.0, which I hope to be released this month.

from farkle.

teo-tsirpanis avatar teo-tsirpanis commented on September 27, 2024

By the way @pmarflee, you are the first user to open an issue in Farkle's three and a half years of existence, making your code the first publicly documented use of Farkle. I am very glad honored of this.

As I am going to release another major version, any suggestions, bug reports, feature requests, and general feedback of yours would be invaluable for me to understand how Farkle is used. If you have any spare time, you can try out Farkle's latest version at its bleeding edge NuGet feed at https://ci.appveyor.com/nuget/farkle.

In any case, let me know if everything went well with your parser. Thanks for using Farkle and good luck with the Advent of Code challenge. ✌🏻

from farkle.

pmarflee avatar pmarflee commented on September 27, 2024

Thanks for your help @teo-tsirpanis . I was able to get my unit tests to pass using the code you provided. You can find my solutions thus far at https://github.com/pmarflee/AdventOfCode2020FSharp. I expect I'll be making further use of Farkle to help with parsing on the remaining problems.

from farkle.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.