1

Good evening to all! As a fan of regular expressions in Perl, I came up with a question, which I could not answer by googling and searching for myself.

So let me give you a minimal example of my problem:

I have two text-files:

FileA.txt:

aaabbb

FileB.txt:

abbb

I want to check each file-content if it is a word, generated by a specific contextfree language. For example in this case: L={a^nb^n | n > 0}.

Now I have the problem, that the regex from Perl won't work, since it isn't a regular language. For sure I could script a little PDA and check if it terminates.

But is there another way in Perl to solve this problem? Maybe a way to pass a context-free-grammar or sth.?

Otterbein
  • 544
  • 3
  • 12
  • Is the contextfree language fixed or are you reading a specification as input to your program? What does the specification look like, a contex-free grammar? – reinierpost Jun 06 '16 at 13:04
  • 3
    [Perl regular expressions can describe arbitrary context-free languages](http://cs.stackexchange.com/questions/4839/which-languages-do-perl-compatible-regular-expressions-recognize), so yes, this can be done with them. – reinierpost Jun 06 '16 at 13:12
  • @reinierpost : For the Beginning I want to use a set of fixed languages but I'm heading to use a variable set of languages specified in the command line as an argument. I will read the article you linked! Sounds good! – Otterbein Jun 06 '16 at 13:30
  • @reinierpost Thanks for your answer, it took me a great step further! Now I'm able to process a CFG. I'll try around with it to check if it holds on every desired grammar. The upcoming question now is, how I can produce the grammar from a given language. (or if I have to within my case. I'll try around) – Otterbein Jun 06 '16 at 14:27
  • In the Iast sentence you probably mean "parse", not "pass". There is no general way to get a grammar from a language. First of all, it depends on the form in which your language is given. If you have a PDA, then there are conversion algorithms. For a set description like the one in your question, there is no general conversion algorithm. – Peter Leupold Jun 07 '16 at 06:52
  • I actually did ment "pass" but in the sense of passing the grammar to a function which parses it. Hm ... I'm actually stuck at the point you mentioned. Like I said, I'm able to process a CFG given in Greibach NF to produce random words out of its production. But there's no general form for the language-Definitions. Maybe I have to admit that my approach isn't a possible way for this problem. Thanks for your feedback! @Peter Leupold – Otterbein Jun 07 '16 at 07:21
  • So in what form are your languages given? And how do you know they really are context-free? In the set specification you can also describe non-context-free languages like a^n b^n c^n. For context-free languages there are not so many common descriptions besides grammars, PDAs, maybe tree automata. – Peter Leupold Jun 07 '16 at 07:53
  • Well the check if the given language is a CFL happens manually atm. So my script won't get started, if there's no proof for the certain language to be a CFL. For now they're described like above and yes, it's possible to descripte even context-sensitive-languages. Actually I thought of asking step by step for the PDA-transitions. Unfortunately this would take some time for bigger machines. But maybe I could speed it up by reading them from a file or sth. I think this could be a way. Again, a big thanks!! – Otterbein Jun 07 '16 at 08:13

0 Answers0