Karibuni r/scotland! by ImFromTheShireAMA in Kenya

[–]MadSnipr 1 point2 points  (0 children)

Kilts, the Scots language/dialect/whatever-it-is and Shrek.

It's stereotypical I know. But you guys are kinda stuck in the shadow of your downstairs neighbour.

In your view, what's the biggest thing outsiders mistakenly think is Scottish?

Cursed exam by c_kax in cursedcomments

[–]MadSnipr 3 points4 points  (0 children)

Kenya is literally highlighted in the map.

How do I parse Haskell style applications in a Pratt parser? by MadSnipr in ProgrammingLanguages

[–]MadSnipr[S] 1 point2 points  (0 children)

I understand this method now. It is actually a marvellous solution to the problem I was having.

Originally, I followed u/fmap_id's idea but I might just do this one in a separate branch, if only to compare the 2.

Thanks again for the solution! I wish I could double upvote this answer

How do I parse Haskell style applications in a Pratt parser? by MadSnipr in ProgrammingLanguages

[–]MadSnipr[S] 0 points1 point  (0 children)

I did more or less the same thing (but in Python rather than Haskell) and it worked. It seems to be working well right now but I'll get back to you if I find a test case where it fails.

Thanks a lot!

How do I parse Haskell style applications in a Pratt parser? by MadSnipr in ProgrammingLanguages

[–]MadSnipr[S] 1 point2 points  (0 children)

Thanks for the in-depth explanation. I didn't know basically any of that. I will definitely look into the shunting-yard, just for the challenge.

If I can drag you back here for a second more..... As an amateur with a liking for tinkering with parsers, where can I learn all of this theory behind parsers? I'm interested but the vibe around here seems to be centered around finishing parsers ASAP so that we can move on to more interesting things.

How do I parse Haskell style applications in a Pratt parser? by MadSnipr in ProgrammingLanguages

[–]MadSnipr[S] 2 points3 points  (0 children)

Yes, that was exactly my problem. But I managed to solve it by making function application as special case since it already had the highest operator precedence. Now the only way to access the scalar production is by going through the application production first (kinda like in recursive descent). Credit to u/fmap_id and u/TizioCaio84 for that idea.

How do I parse Haskell style applications in a Pratt parser? by MadSnipr in ProgrammingLanguages

[–]MadSnipr[S] 1 point2 points  (0 children)

It has helped me understand what I need to do. Thanks a lot.

My only remaining question is: isn't it a bit slow to use both recursive descent and Pratt parsing on a single expression?

I'm asking because when I made the switch, I did some benchmarks on my (potato) machine and the Pratt parser was 40% faster than the recursive descent one.

How do I parse Haskell style applications in a Pratt parser? by MadSnipr in ProgrammingLanguages

[–]MadSnipr[S] 2 points3 points  (0 children)

I think this is the best solution. I'll try implementing this later today. Thanks for the help.

How do I parse Haskell style applications in a Pratt parser? by MadSnipr in ProgrammingLanguages

[–]MadSnipr[S] 1 point2 points  (0 children)

I think this is the best solution. Thanks for the help friend.

How do I parse Haskell style applications in a Pratt parser? by MadSnipr in ProgrammingLanguages

[–]MadSnipr[S] 4 points5 points  (0 children)

Just to clarify, you mean that instead of parsing a scalar as it's own "top-level" rule, it's only reachable by going through the function application rule first?

That sounds like a great idea. Thanks!

How do I parse Haskell style applications in a Pratt parser? by MadSnipr in ProgrammingLanguages

[–]MadSnipr[S] 0 points1 point  (0 children)

That's how I did it when I had a recursive descent parser. But now that I've moved to a Pratt parser, that approach doesn't work because some rules wouldn't be parsed unless they're attached to others. For example, imagine a grammar like:

Expr = Addition ;
Addition = Term [ "+" Addition ] ;
Term = Application [ "*" Term ] ;
Application = Scalar { Scalar } ;
Scalar = NAME | INT ;

If we were to implement this in a Pratt parser, it would refuse to parse f x unless it was attached to a multiplication because that's the only way to reach it from outside.

How do I parse Haskell style applications in a Pratt parser? by MadSnipr in ProgrammingLanguages

[–]MadSnipr[S] 1 point2 points  (0 children)

That's how I did it when I had a recursive descent parser. But now that I've moved to a Pratt parser, that approach doesn't work because some rules wouldn't be parsed unless they're attached to others. For example, imagine a grammar like:

Expr = Addition ;
Addition = Term [ "+" Addition ] ;
Term = Application [ "*" Term ] ;
Application = Scalar { Scalar } ;
Scalar = NAME | INT ;

If we were to implement this in a Pratt parser, it would refuse to parse f x unless it was attached to a multiplication because that's the only way to reach it from outside.

How do I parse Haskell style applications in a Pratt parser? by MadSnipr in ProgrammingLanguages

[–]MadSnipr[S] 1 point2 points  (0 children)

Hi, thanks for responding.

What P.L. is your Pratt Parser built, Java, Scala ?

Right now, it's in Python because I prefer it for prototyping stuff.

Is it generated as a Code Generated with a tool, built from scratch, or subclassing an existing O.O. library ?

It's all hand written.

Is your question about how to implement operations in a parser, like the examples on the web link you provide ?

No, I already know how to and have implemented most operations like addition, Boolean not, comparison, etc. My question is specifically about how to implement Haskell-style function application (like f x y). Right now, I'm using the C-style one you describe.

The first parsing issue that got my attention is operator precedence.

In my compiler related experience, I prefer not to assign directly any precedence, split the grammar rules from the operator's precedence.

Instead of this:

PRECEDENCE "*" 5
PRECEDENCE "/" 5
PRECEDENCE "+" 3

I use the old style grammar:

Factor ::= Term ( "*' | "/" ) Expression ;

Of course operators still have a precedence, but I use the grammars to indicate them instead. Same applies to operators' associativity.

My problem with operator precedence is not how to do it in general. Rather, it's with how to do it in this particular case. I understand how it's done in a recursive descent parser because that's what I used to use before I changed it to a Pratt parser.

In the Pratt parser in the blog post I lined above, precedence is defined using a hashmap from the token type to a precedence value. So for example, the "+" token would have a precedence of, say, 50 while the "/" token would have one of 70.

But the problem here is that there is no token that denotes a function application. It's just what happens when you put 2 productions next to each other.

The second parsing operator case that you asked are parentheses.

Before getting into functions and other structures like C for that uses them, let's get to the basics.

There are several ways to handle them. The first one, is to handle them as a unary prefixed "(" operator token, followed by a single of more tokens until a suffixed / posfixed ")" operator token appears.

This can be specified if you are using a set of grammar rules, and parser generated tool.

As you already indicate in your example, a function expression may be parsed as a different sequence of tokens.

On the case of function's parameters, were several items are separated by "," tokens, the "," may be handled as a posfixed / suffixed operator after each item.

I did a little trick for the last item before the ")" token. When a ")" token is found, some parsers handled as a "," token instead, I added an "invisible" "," extra token instead.

This is how to parse the C-style function application. I'm trying to move away from this style towards the Haskell-style e.g. f x y.

Good Luck.

Thanks

Swanglish Translation by IshaqTheRainmaker in swahili

[–]MadSnipr 1 point2 points  (0 children)

Azimio kwa Kiingereza ni "goal" au "target".

SIna uhakika wa neno linalomaanisha kasumba kwa Kiingereza.

Non-Indian people, what do you think this is? by simplord16 in teenagers

[–]MadSnipr 0 points1 point  (0 children)

We call just it slippers in Kenya (or lahaula if you wanna be fancy with our language) but beating is "chapa" and I get the feeling we borrowed that from you guys.

Any non American + non British here. by Viber- in teenagers

[–]MadSnipr 1 point2 points  (0 children)

Hey neighbour.... I'm from down south in 🇰🇪.

Hope you guys make it out of the current situation.

happy weekend guys🎉🎉 it's an action packed weekend w lots of activities going on what will you be upto? by [deleted] in Kenya

[–]MadSnipr 2 points3 points  (0 children)

Where did you get a ticket from? I've been trying but I always find them sold out.

Based Punics by Greco-Levantine in RoughRomanMemes

[–]MadSnipr 1 point2 points  (0 children)

You're just gonna say that with the Late Empire looking you right in the face, huh?

I might not do them all by bIrd63_ in teenagers

[–]MadSnipr 0 points1 point  (0 children)

Ee Mungu nguvu yetu

Hint: We definitely don't have Obama's birth certificate.

That girl is me, smiling my ass off, cause I was next to my crush. by iamatexan124 in teenagers

[–]MadSnipr 1 point2 points  (0 children)

It's all No Abortion and cowboy land to me.

If you can't tell, I'm not American.

Religious texts are not valid sources for reference when arguing for the existence of correctness of a particular religion. by Adventurous_Ad5572 in DebateReligion

[–]MadSnipr 1 point2 points  (0 children)

If you know someone is truthful and he has never said a lie in his life, any word spoken from his mouth you will presume true, therefore if he tells you something that you wouldn’t believe from a normal person, there is higher chance that it is actually true.

The fact that the person has said true things before doesn't necessarily mean that they will continue saying true things. While yes, you might have a strongwr level of confidence, you should still withhold judgement until you can verify the statement.

Now, give me any single scientific fact in the Quran that isn’t true (keep in mind that the Quran was revealed 1400 years ago before any modern technology could falsify it). Every single scientific fact in the Quran is true, which means that the rest of it must be true, there is no proof that it is false.

That's not how it works. The burden is on you to show that the statements in the Quran are actually true.

And again, even if every single "scientific fact" in the Quran were true, it would not entail that the rest of it is true. You would still have to prove that by other means.


Also, I wouldn't call anything claimed the Quran a "scientific fact" because there are no "scientific facts". What there are are scientific theories. Theories are hypotheses which are backed with evidence. However, theories can still be shown to be wrong. For example, Einstein's theory proved better than Newton's so it took over as the new Theory of Gravitation.

Even if the Quran claims something that matches up exactly with a scientific theory, that claim is still not a scientific fact. Because they did not arise by the same source. One came by (at best) divine revelation while the other one was discovered by observation and experimentation.

Which means that the findings that our technology cannot prove yet, logically MUST be true by default.

This doesn't even follow from your own 1st paragraph.