Click here to Skip to main content
15,904,346 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralRe: mmmm sashimi sushi for breakfast. What do you eat for brain food? Pin
honey the codewitch7-May-19 8:21
mvahoney the codewitch7-May-19 8:21 
GeneralRe: mmmm sashimi sushi for breakfast. What do you eat for brain food? Pin
Eddy Vluggen7-May-19 9:10
professionalEddy Vluggen7-May-19 9:10 
GeneralRe: mmmm sashimi sushi for breakfast. What do you eat for brain food? Pin
honey the codewitch7-May-19 9:25
mvahoney the codewitch7-May-19 9:25 
GeneralRe: mmmm sashimi sushi for breakfast. What do you eat for brain food? Pin
Eddy Vluggen7-May-19 9:31
professionalEddy Vluggen7-May-19 9:31 
GeneralRe: mmmm sashimi sushi for breakfast. What do you eat for brain food? Pin
honey the codewitch7-May-19 9:42
mvahoney the codewitch7-May-19 9:42 
GeneralRe: mmmm sashimi sushi for breakfast. What do you eat for brain food? Pin
Eddy Vluggen7-May-19 23:14
professionalEddy Vluggen7-May-19 23:14 
GeneralRe: mmmm sashimi sushi for breakfast. What do you eat for brain food? Pin
honey the codewitch8-May-19 4:06
mvahoney the codewitch8-May-19 4:06 
GeneralRe: mmmm sashimi sushi for breakfast. What do you eat for brain food? Pin
honey the codewitch8-May-19 4:46
mvahoney the codewitch8-May-19 4:46 
alright, after reading my other reply, and asking any other questions you have, let's continue:

recall our grammar

binop → num + num
      | num - num
      | num * num
      | num / num


We won't be using this one. There's no operator precedence in it. and no parenthesis

Expr = Expr '[\+\-]' Term | Term
Term = Term '[/\*] Factor | Factor
Factor = '\(' Expr '\)' | int
int = '[0-9]+'


Woah. This is more practical but
a) it's not a straight CFG
b) it looks messy and confusing

Let's take care of that.

First, all the single quoted strings are regular expressions

those are our "terminals" - they represent the leaves of the parse tree will be parsing.

CFGs care about terminals, but NOT their definitions.

Let's rewrite this to be purely a CFG - we'll separate the terminal definitions out and leave them aside:

Expr = Expr add Term | Term
Term = Term mul Factor | Factor
Factor = lparen Expr rparen | int


There we go. All the terminals are lowercase, and the non-terminals are title case (not required, just a convention we'll use here). The definitions of the terminals are gone, replaced by labels.

How can a CFG tell what's a terminal and a nonterminal just from this? simple - a non-terminal is anything that appears on the left hand side

Anyway, next we will break out this | (or) expressions into separate rules


Expr → Expr add Term
Expr → Term
Term → Term mul Factor 
Term → Factor
Factor → lparen Expr rparen
Factor → int


This is how a CFG likes it. Whew. One more major step, and it's a little involved.

The last issue here is that this is left recursive. The rules reference their left non-terminal on the leftmost part of the right hand side. Some parser algorithms (the LR family is an example) can handle this but an LL can't - it would loop infinitely.

So now we have to refactor this CFG. It can be done programmatically. (My code does this)

Let's take a look at the first two rules

Expr → Expr add Term
Expr → Term


For each rule where the non-terminal on the left (Expr) of the arrow is the same as the left-side of a production on the right-hand side of the arrow (Expr add Term), we take the part of the rule without the recursive bit (which leaves us with "add Term") and move it down into its own new rule (we'll call it Expr').

Expr' → add Term


Now, after each of the new productions, add Expr' to the end.

Expr' → add Term Expr'


Still not done. Now add an extra rule with an ε (epsilon) which just means an "empty string" - matches without consuming or examining input.

Expr' → ε


Good. Now we must fix up the original Expr rules. Here, we take all of the right-hand sides that didn't start with Expr, and add Expr' to the end of them.

Expr → Term Expr'


If we do this for the Term productions as well, we get the following grammar:

Expr   → Term Expr'
Expr'add Term Expr'
Expr'  → ε
Term   → Factor Term'
Term'  → mul Factor Term'
Term'  → ε
Factor → lparen Expr rparen
Factor → int



TADA!, and we're done so far. I'll take this opportunity to stop and wait if you have questions.

I've covered a lot here so far. Some of this was ripped from this wonderful LL(1) tutorial

So once you respond and check in, we'll keep going.
When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.

GeneralRe: mmmm sashimi sushi for breakfast. What do you eat for brain food? Pin
Eddy Vluggen8-May-19 7:18
professionalEddy Vluggen8-May-19 7:18 
GeneralRe: mmmm sashimi sushi for breakfast. What do you eat for brain food? Pin
honey the codewitch8-May-19 8:02
mvahoney the codewitch8-May-19 8:02 
GeneralRe: mmmm sashimi sushi for breakfast. What do you eat for brain food? Pin
Eddy Vluggen10-May-19 22:53
professionalEddy Vluggen10-May-19 22:53 
GeneralRe: mmmm sashimi sushi for breakfast. What do you eat for brain food? Pin
honey the codewitch11-May-19 7:16
mvahoney the codewitch11-May-19 7:16 
GeneralRe: mmmm sashimi sushi for breakfast. What do you eat for brain food? Pin
Eddy Vluggen11-May-19 23:30
professionalEddy Vluggen11-May-19 23:30 
GeneralRe: mmmm sashimi sushi for breakfast. What do you eat for brain food? Pin
honey the codewitch11-May-19 23:46
mvahoney the codewitch11-May-19 23:46 
GeneralRe: mmmm sashimi sushi for breakfast. What do you eat for brain food? Pin
Eddy Vluggen12-May-19 0:05
professionalEddy Vluggen12-May-19 0:05 
GeneralRe: mmmm sashimi sushi for breakfast. What do you eat for brain food? Pin
Mark_Wallace7-May-19 9:51
Mark_Wallace7-May-19 9:51 
GeneralRe: mmmm sashimi sushi for breakfast. What do you eat for brain food? Pin
Mycroft Holmes7-May-19 12:55
professionalMycroft Holmes7-May-19 12:55 
GeneralRe: mmmm sashimi sushi for breakfast. What do you eat for brain food? Pin
honey the codewitch7-May-19 14:13
mvahoney the codewitch7-May-19 14:13 
GeneralRe: mmmm sashimi sushi for breakfast. What do you eat for brain food? Pin
Christian Graus7-May-19 15:01
protectorChristian Graus7-May-19 15:01 
GeneralRe: mmmm sashimi sushi for breakfast. What do you eat for brain food? Pin
honey the codewitch7-May-19 16:33
mvahoney the codewitch7-May-19 16:33 
GeneralRe: mmmm sashimi sushi for breakfast. What do you eat for brain food? Pin
Christian Graus7-May-19 16:58
protectorChristian Graus7-May-19 16:58 
Rantfinally dropping firefox - rant with happy ending Pin
lopatir7-May-19 5:03
lopatir7-May-19 5:03 
GeneralRe: finally dropping firefox - rant with happy ending Pin
Slacker0077-May-19 5:19
professionalSlacker0077-May-19 5:19 
QuestionRe: finally dropping firefox - rant with happy ending Pin
megaadam7-May-19 5:32
professionalmegaadam7-May-19 5:32 
AnswerRe: finally dropping firefox - rant with happy ending Pin
lopatir7-May-19 5:42
lopatir7-May-19 5:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.