Finding Distinct Terminal and Nonterminals SymbolsGrammar production class implementation in C#Polynomial size CFG such that each terminal in a word occurs even number of times (large alphabet)Constructing a Follow SetContext-free grammars versus context-sensitive grammars?How to parse CFG's with arbitrary numbers of neighbors?Difference between Context-sensitive grammar and Context-free grammarIs C++ context-free or context-sensitive?Ambiguous non-terminal in GLRHow can I check that the language of one context-free grammar is a subset of a second context-free grammar?ECMAScript 2017, 5 Notational Conventions: What are productions, terminal and nonterminal symbols?
What is the tangent at a sharp point on a curve?
I keep switching characters, how do I stop?
Hashing password to increase entropy
Can a Knock spell open the door to Mordenkainen's Magnificent Mansion?
Highest stage count that are used one right after the other?
How do you justify more code being written by following clean code practices?
Has the laser at Magurele, Romania reached a tenth of the Sun's power?
Asserting that Atheism and Theism are both faith based positions
What is the period/term used describe Giuseppe Arcimboldo's style of painting?
A seasonal riddle
Mortal danger in mid-grade literature
What is the probability that the nth card becomes the top card after shuffling a certain way?
Why is "la Gestapo" feminine?
What is it called when someone votes for an option that's not their first choice?
Capacitor electron flow
Can you describe someone as luxurious? As in someone who likes luxurious things?
Make a Bowl of Alphabet Soup
Is there any common country to visit for persons holding UK and Schengen visas?
Non-Borel set in arbitrary metric space
Sort with assumptions
Does capillary rise violate hydrostatic paradox?
Why does the Persian emissary display a string of crowned skulls?
C++ lambda syntax
Magnifying glass in hyperbolic space
Finding Distinct Terminal and Nonterminals Symbols
Grammar production class implementation in C#Polynomial size CFG such that each terminal in a word occurs even number of times (large alphabet)Constructing a Follow SetContext-free grammars versus context-sensitive grammars?How to parse CFG's with arbitrary numbers of neighbors?Difference between Context-sensitive grammar and Context-free grammarIs C++ context-free or context-sensitive?Ambiguous non-terminal in GLRHow can I check that the language of one context-free grammar is a subset of a second context-free grammar?ECMAScript 2017, 5 Notational Conventions: What are productions, terminal and nonterminal symbols?
I am currently working on writing two functions that will be used to return lists of all distinct terminals that appear in a given grammar.
Here is the type for Grammar :
data Grammar = Grammar [Prod]
deriving (Show, Eq)
Here is what I have so far :
terminals :: Grammar -> [String]
terminals ts = nub [ x | x <-ts]
nonterms :: Grammar -> [String]
nonterms nt = nub [ x | x <- nt]
Where the grammar that is being placed into the function would be something like this (3 Examples) :
g4 = "P -> true ; P -> false; P -> not P ; P -> P and P"
g5 = "P -> P & N; P -> N; N -> ~ N; N -> t"
g6 = "P -> N & P; P -> N; N -> ~ N; N -> t"
However though, the functions that I am using do not work because GHCI could not match expected type ‘[String]’ with actual type ‘Grammar’.
I am going off of the basic principles of Context-free grammars (CFGs), that a context-free grammar is
G = (T,N,P,S)
Where T is a set T of terminal symbols ("tokens"), a set N of nonterminal symbols, a set P of productions, and a start symbol S.
How could I use / write two functions that could return lists of all the distinct terminals (respectively nonterminals) that appear in a given grammar. Three examples of grammars above. Thank you.
haskell context-free-grammar
add a comment |
I am currently working on writing two functions that will be used to return lists of all distinct terminals that appear in a given grammar.
Here is the type for Grammar :
data Grammar = Grammar [Prod]
deriving (Show, Eq)
Here is what I have so far :
terminals :: Grammar -> [String]
terminals ts = nub [ x | x <-ts]
nonterms :: Grammar -> [String]
nonterms nt = nub [ x | x <- nt]
Where the grammar that is being placed into the function would be something like this (3 Examples) :
g4 = "P -> true ; P -> false; P -> not P ; P -> P and P"
g5 = "P -> P & N; P -> N; N -> ~ N; N -> t"
g6 = "P -> N & P; P -> N; N -> ~ N; N -> t"
However though, the functions that I am using do not work because GHCI could not match expected type ‘[String]’ with actual type ‘Grammar’.
I am going off of the basic principles of Context-free grammars (CFGs), that a context-free grammar is
G = (T,N,P,S)
Where T is a set T of terminal symbols ("tokens"), a set N of nonterminal symbols, a set P of productions, and a start symbol S.
How could I use / write two functions that could return lists of all the distinct terminals (respectively nonterminals) that appear in a given grammar. Three examples of grammars above. Thank you.
haskell context-free-grammar
Grammar
isn't a type that's built in to Haskell. Please post its definition.
– Joseph Sible
Mar 7 at 2:50
Sorry about that. Just fixed it.
– yee
Mar 7 at 2:59
add a comment |
I am currently working on writing two functions that will be used to return lists of all distinct terminals that appear in a given grammar.
Here is the type for Grammar :
data Grammar = Grammar [Prod]
deriving (Show, Eq)
Here is what I have so far :
terminals :: Grammar -> [String]
terminals ts = nub [ x | x <-ts]
nonterms :: Grammar -> [String]
nonterms nt = nub [ x | x <- nt]
Where the grammar that is being placed into the function would be something like this (3 Examples) :
g4 = "P -> true ; P -> false; P -> not P ; P -> P and P"
g5 = "P -> P & N; P -> N; N -> ~ N; N -> t"
g6 = "P -> N & P; P -> N; N -> ~ N; N -> t"
However though, the functions that I am using do not work because GHCI could not match expected type ‘[String]’ with actual type ‘Grammar’.
I am going off of the basic principles of Context-free grammars (CFGs), that a context-free grammar is
G = (T,N,P,S)
Where T is a set T of terminal symbols ("tokens"), a set N of nonterminal symbols, a set P of productions, and a start symbol S.
How could I use / write two functions that could return lists of all the distinct terminals (respectively nonterminals) that appear in a given grammar. Three examples of grammars above. Thank you.
haskell context-free-grammar
I am currently working on writing two functions that will be used to return lists of all distinct terminals that appear in a given grammar.
Here is the type for Grammar :
data Grammar = Grammar [Prod]
deriving (Show, Eq)
Here is what I have so far :
terminals :: Grammar -> [String]
terminals ts = nub [ x | x <-ts]
nonterms :: Grammar -> [String]
nonterms nt = nub [ x | x <- nt]
Where the grammar that is being placed into the function would be something like this (3 Examples) :
g4 = "P -> true ; P -> false; P -> not P ; P -> P and P"
g5 = "P -> P & N; P -> N; N -> ~ N; N -> t"
g6 = "P -> N & P; P -> N; N -> ~ N; N -> t"
However though, the functions that I am using do not work because GHCI could not match expected type ‘[String]’ with actual type ‘Grammar’.
I am going off of the basic principles of Context-free grammars (CFGs), that a context-free grammar is
G = (T,N,P,S)
Where T is a set T of terminal symbols ("tokens"), a set N of nonterminal symbols, a set P of productions, and a start symbol S.
How could I use / write two functions that could return lists of all the distinct terminals (respectively nonterminals) that appear in a given grammar. Three examples of grammars above. Thank you.
haskell context-free-grammar
haskell context-free-grammar
edited Mar 7 at 7:56
Michael Litchard
1,95521541
1,95521541
asked Mar 7 at 1:27
yeeyee
235
235
Grammar
isn't a type that's built in to Haskell. Please post its definition.
– Joseph Sible
Mar 7 at 2:50
Sorry about that. Just fixed it.
– yee
Mar 7 at 2:59
add a comment |
Grammar
isn't a type that's built in to Haskell. Please post its definition.
– Joseph Sible
Mar 7 at 2:50
Sorry about that. Just fixed it.
– yee
Mar 7 at 2:59
Grammar
isn't a type that's built in to Haskell. Please post its definition.– Joseph Sible
Mar 7 at 2:50
Grammar
isn't a type that's built in to Haskell. Please post its definition.– Joseph Sible
Mar 7 at 2:50
Sorry about that. Just fixed it.
– yee
Mar 7 at 2:59
Sorry about that. Just fixed it.
– yee
Mar 7 at 2:59
add a comment |
1 Answer
1
active
oldest
votes
terminals :: Grammar -> [String]
terminals ts = nub [ x | x <-ts]
Your function terminals
is typed as returning a [String]
. Its implementation returns the result of nub
. nub
has type Eq a => [a] -> [a]
, so for it to return a [String]
, its input must be a [String]
as well. This implies that [ x | x <-ts]
must be of type [String]
, and then that x
must be of type String
. Doing x <- ts
in a list comprehension means that ts
must be a list of whatever type x
is, so in this case, ts
must be [String]
.
Now the problem becomes apparent. Given the type of the output of your function and its implementation, ts
must be of type [String]
, but given the type of the input of your function, ts
must be of type Grammar
. Since [String]
and Grammar
are different types, this causes the type error that you're seeing.
Side note: [ x | x <-ts]
is exactly equivalent to just ts
, and [ x | x <- nt]
is exactly equivalent to just nt
.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55034680%2ffinding-distinct-terminal-and-nonterminals-symbols%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
terminals :: Grammar -> [String]
terminals ts = nub [ x | x <-ts]
Your function terminals
is typed as returning a [String]
. Its implementation returns the result of nub
. nub
has type Eq a => [a] -> [a]
, so for it to return a [String]
, its input must be a [String]
as well. This implies that [ x | x <-ts]
must be of type [String]
, and then that x
must be of type String
. Doing x <- ts
in a list comprehension means that ts
must be a list of whatever type x
is, so in this case, ts
must be [String]
.
Now the problem becomes apparent. Given the type of the output of your function and its implementation, ts
must be of type [String]
, but given the type of the input of your function, ts
must be of type Grammar
. Since [String]
and Grammar
are different types, this causes the type error that you're seeing.
Side note: [ x | x <-ts]
is exactly equivalent to just ts
, and [ x | x <- nt]
is exactly equivalent to just nt
.
add a comment |
terminals :: Grammar -> [String]
terminals ts = nub [ x | x <-ts]
Your function terminals
is typed as returning a [String]
. Its implementation returns the result of nub
. nub
has type Eq a => [a] -> [a]
, so for it to return a [String]
, its input must be a [String]
as well. This implies that [ x | x <-ts]
must be of type [String]
, and then that x
must be of type String
. Doing x <- ts
in a list comprehension means that ts
must be a list of whatever type x
is, so in this case, ts
must be [String]
.
Now the problem becomes apparent. Given the type of the output of your function and its implementation, ts
must be of type [String]
, but given the type of the input of your function, ts
must be of type Grammar
. Since [String]
and Grammar
are different types, this causes the type error that you're seeing.
Side note: [ x | x <-ts]
is exactly equivalent to just ts
, and [ x | x <- nt]
is exactly equivalent to just nt
.
add a comment |
terminals :: Grammar -> [String]
terminals ts = nub [ x | x <-ts]
Your function terminals
is typed as returning a [String]
. Its implementation returns the result of nub
. nub
has type Eq a => [a] -> [a]
, so for it to return a [String]
, its input must be a [String]
as well. This implies that [ x | x <-ts]
must be of type [String]
, and then that x
must be of type String
. Doing x <- ts
in a list comprehension means that ts
must be a list of whatever type x
is, so in this case, ts
must be [String]
.
Now the problem becomes apparent. Given the type of the output of your function and its implementation, ts
must be of type [String]
, but given the type of the input of your function, ts
must be of type Grammar
. Since [String]
and Grammar
are different types, this causes the type error that you're seeing.
Side note: [ x | x <-ts]
is exactly equivalent to just ts
, and [ x | x <- nt]
is exactly equivalent to just nt
.
terminals :: Grammar -> [String]
terminals ts = nub [ x | x <-ts]
Your function terminals
is typed as returning a [String]
. Its implementation returns the result of nub
. nub
has type Eq a => [a] -> [a]
, so for it to return a [String]
, its input must be a [String]
as well. This implies that [ x | x <-ts]
must be of type [String]
, and then that x
must be of type String
. Doing x <- ts
in a list comprehension means that ts
must be a list of whatever type x
is, so in this case, ts
must be [String]
.
Now the problem becomes apparent. Given the type of the output of your function and its implementation, ts
must be of type [String]
, but given the type of the input of your function, ts
must be of type Grammar
. Since [String]
and Grammar
are different types, this causes the type error that you're seeing.
Side note: [ x | x <-ts]
is exactly equivalent to just ts
, and [ x | x <- nt]
is exactly equivalent to just nt
.
answered Mar 7 at 3:07
Joseph SibleJoseph Sible
6,87131335
6,87131335
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55034680%2ffinding-distinct-terminal-and-nonterminals-symbols%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Grammar
isn't a type that's built in to Haskell. Please post its definition.– Joseph Sible
Mar 7 at 2:50
Sorry about that. Just fixed it.
– yee
Mar 7 at 2:59