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?













1















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.










share|improve this question
























  • 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















1















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.










share|improve this question
























  • 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













1












1








1








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.










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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

















  • 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












1 Answer
1






active

oldest

votes


















2














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.






share|improve this answer






















    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
    );



    );













    draft saved

    draft discarded


















    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









    2














    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.






    share|improve this answer



























      2














      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.






      share|improve this answer

























        2












        2








        2







        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.






        share|improve this answer













        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 7 at 3:07









        Joseph SibleJoseph Sible

        6,87131335




        6,87131335





























            draft saved

            draft discarded
















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Save data to MySQL database using ExtJS and PHP [closed]2019 Community Moderator ElectionHow can I prevent SQL injection in PHP?Which MySQL data type to use for storing boolean valuesPHP: Delete an element from an arrayHow do I connect to a MySQL Database in Python?Should I use the datetime or timestamp data type in MySQL?How to get a list of MySQL user accountsHow Do You Parse and Process HTML/XML in PHP?Reference — What does this symbol mean in PHP?How does PHP 'foreach' actually work?Why shouldn't I use mysql_* functions in PHP?

            Compiling GNU Global with universal-ctags support Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Tags for Emacs: Relationship between etags, ebrowse, cscope, GNU Global and exuberant ctagsVim and Ctags tips and trickscscope or ctags why choose one over the other?scons and ctagsctags cannot open option file “.ctags”Adding tag scopes in universal-ctagsShould I use Universal-ctags?Universal ctags on WindowsHow do I install GNU Global with universal ctags support using Homebrew?Universal ctags with emacsHow to highlight ctags generated by Universal Ctags in Vim?

            Add ONERROR event to image from jsp tldHow to add an image to a JPanel?Saving image from PHP URLHTML img scalingCheck if an image is loaded (no errors) with jQueryHow to force an <img> to take up width, even if the image is not loadedHow do I populate hidden form field with a value set in Spring ControllerStyling Raw elements Generated from JSP tagds with Jquery MobileLimit resizing of images with explicitly set width and height attributeserror TLD use in a jsp fileJsp tld files cannot be resolved