LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-28-2016, 01:27 AM   #1
andrei.n
LQ Newbie
 
Registered: Dec 2014
Distribution: Fedora, Debian, lfs
Posts: 16

Rep: Reputation: 0
String structure using BNF


Is there a way to create a tree representation of a string from the string and the BNF description of its syntax?

It seems that most tools don't use the BNF directly, but is it possible to do it without intermediate steps?

Thanks.
 
Old 04-28-2016, 01:47 AM   #2
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,264
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
That is the essential core function of a parser.

Flex + Bison

Last edited by astrogeek; 04-28-2016 at 01:48 AM.
 
Old 04-28-2016, 01:48 AM   #3
andrei.n
LQ Newbie
 
Registered: Dec 2014
Distribution: Fedora, Debian, lfs
Posts: 16

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by astrogeek View Post
Flex + Bison
But these are external tools...
 
Old 04-28-2016, 01:51 AM   #4
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,264
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
External to what?

I am not sure I understand your requirement.
 
1 members found this post helpful.
Old 04-28-2016, 01:55 AM   #5
andrei.n
LQ Newbie
 
Registered: Dec 2014
Distribution: Fedora, Debian, lfs
Posts: 16

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by astrogeek View Post
External to what?

I am not sure I understand your requirement.
Let's say I want to do it in pure C, without using any external libraries and programs.
 
Old 04-28-2016, 02:05 AM   #6
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,264
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
There is no facility in the C/C++ languages to do that, so you would have to write your own handlers for doing it.

You can hand-write lexers/parsers, many people do, it is a well understood but complex task. Unless your grammer is extremely simple with few symbols/tokens, it would likely take quite a bit of work.

How "complex" a grammer do you need to parse?
 
Old 04-28-2016, 02:09 AM   #7
andrei.n
LQ Newbie
 
Registered: Dec 2014
Distribution: Fedora, Debian, lfs
Posts: 16

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by astrogeek View Post
How "complex" a grammer do you need to parse?
Not really complex. Something that can be represented by a BNF, since I'd like to have BNF as one of the in parameters.
 
Old 04-28-2016, 02:30 AM   #8
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,264
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
So, the grammer itself is not fixed?

What you are describing then, is a general purpose parser then, where you give it a grammer (the BNF) and a string and ask it to parse the string into a sentence, an AST according to that grammer.

That is not a new idea, but I am not aware of a successful implementation. It is a very complex proposition!

As it happens, I am working on a grammer/parser project at this time and in my recent reading I saw some in depth discussion of that, a general purpose parser, somewhere... ah, here and here (Both excellent books on the subject by the way!).

I'll try to have a fresh look at that tomorrow and post any useful nuggets I find. But as I recall it poses several very big, if not insurmountable difficulties for all but the most trivial grammers.

Last edited by astrogeek; 04-28-2016 at 02:32 AM.
 
Old 04-28-2016, 02:43 AM   #9
andrei.n
LQ Newbie
 
Registered: Dec 2014
Distribution: Fedora, Debian, lfs
Posts: 16

Original Poster
Rep: Reputation: 0
Thanks for the links, it looks interesting.

Quote:
Originally Posted by astrogeek View Post
But as I recall it poses several very big, if not insurmountable difficulties for all but the most trivial grammers.
That's what I'd like to know more about in order to not lose time trying to solve unsolvable problems...
 
Old 04-28-2016, 02:54 AM   #10
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,264
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
The author of those two books is pretty much the authority on the subject I think.

He makes an earlier edition of one of them available for download here. If you are not already very familiar with the subject, this would be an excellent place to start reading. This book does not include any code, but you will understand the BNF and the basic problems of parsing to a syntax tree after reading it!

Good luck!
 
Old 04-28-2016, 06:26 AM   #11
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
There certainly are parsers out there, such as Perl's Parse::RecDescent war-horse, that can "parse on the fly," but the ordinary procedure is to first compile the grammar, then compile the source-code generated by the grammar processor. Since in most applications the grammar is fixed, this produces a very efficient parsing engine.

And ... you really don't want to "do it yourself in 'C'" ... unless you are a graduate student.
 
Old 04-28-2016, 08:36 AM   #12
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,225

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
Quote:
Originally Posted by andrei.n View Post
Not really complex. Something that can be represented by a BNF, since I'd like to have BNF as one of the in parameters.
Can BNF be represented as BNF?

It sounds like you're asking for a lexer and parser that would lex and parse a BNF, and then output another lexer and parser from that. I don't think the BNF has enough information for that.

Last edited by dugan; 04-28-2016 at 08:46 AM.
 
Old 04-28-2016, 12:57 PM   #13
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
Yes, there is a BNF grammar for the BNF grammar language . . .
 
Old 05-02-2016, 01:11 PM   #14
X-LFS-2010
Member
 
Registered: Apr 2016
Posts: 510

Rep: Reputation: 58
if you want tree representations and to do parsing work with them, try:

AntLR: powerful graphic interface to parsing, now supported/used by Apple, GPL free - it's a good bet if you have something big in mind you should consider using it

bnf2xml uses simple bnf and using that scans input text makes a text xml "tree" (not a highly developed app, just a small typical unix "text filter"). one should maybe use awk unless the parsing BNF spec is too complicated for awk/grep OR for C if regex output tree is too large for those to handle reasonably well or is cumbersome (regex stores multiple results haphazardously - unless you know regex well it might take many tries to get how it deals with storing multiple hits and where they will be in memory)

there are MANY bnf parsers out there that handle extended EBNF sytax. (most all) require you build them into your C application and use a custom interface to glean the results found by them.
 
1 members found this post helpful.
Old 05-02-2016, 01:13 PM   #15
X-LFS-2010
Member
 
Registered: Apr 2016
Posts: 510

Rep: Reputation: 58
BNF grammar is simple, and was used in many books for describing C and C++ syntax

but its NOT necessarily the best language see AntLR about languages and complications arising from the choice of their use

Cduce is another interesting parser - this class of parser is geared toward HTML+XML web server use (it's output is HTML not XML an i think requires intengration into C). there are many new such products.

Last edited by X-LFS-2010; 05-02-2016 at 01:17 PM.
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to assign a string to a structure variable in C? daYz Programming 10 07-06-2022 11:07 AM
Looking for a string in the hd disregarding the file structure. stf92 Linux - Desktop 2 07-20-2014 11:43 AM
[SOLVED] How to Initialize an array of string literals inside a structure Jusctsch Programming 2 11-08-2011 04:22 PM
EBNF to BNF algorithm john_crichton Programming 0 05-29-2007 12:29 PM
BNF Parser generator - how to use it? Enjo Linux - Newbie 0 06-19-2006 12:10 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:12 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration