Visit Our UNIX and Linux User Community


How to match n number of newline character(\n) in lex and yacc


 
Thread Tools Search this Thread
Top Forums Programming How to match n number of newline character(\n) in lex and yacc
# 1  
Old 11-07-2008
How to match n number of newline character(\n) in lex and yacc

Hi ,

I need to develop a parser which should match something like

1. text a=5 " a=20";

2. text a=..."
a=20";

3 text a=..."
a=20
b=34
c=12
";


I have used this regular expression in my Lex file to generate the tokens:

\".\s*.*\s.\"

which matches " followed by anything and a newline etc.
The problem with the above regular expression is that it can take at most 2 new line(\n) characters because the . fails to match (\n) and breaks at that point. so i had to explicitly specify (\s*) to match the newline in between quotes(" "). if my file contains something as:

text a=10 "
b=25
c=30
d= 40
e=11
";
the above regular expression(\".\s*.*\s.\") fails to match it.


Can anyone write the regular expression which can take any any number of new lines inside the quotes (" ") and which will help to solve my problem


Please Help
# 2  
Old 11-10-2008
The <newline> is not matched by the "period" operator. You have to specifically include it. \s Doesn't match a space in lex.
Code:
\"(.|[ \t\v\n])*\"

should work on :
Code:
1: ""
2: "blah blah"
3: "

"
4: " blah "

# 3  
Old 11-13-2008
Hi

Thanks for your reply otheus.

But when i use the regular expression
\"(.|[ \t\v\n])*\"I get a fatal error as "input buffer overflow, can't enlarge buffer because scanner uses REJECT"
# 4  
Old 11-13-2008
Quote:
Originally Posted by vishwa787
Hi

Thanks for your reply otheus.

But when i use the regular expression
\"(.|[ \t\v\n])*\"I get a fatal error as "input buffer overflow, can't enlarge buffer because scanner uses REJECT"
It's been 14+ years since I touched lex. Anyone else want to try here?
# 5  
Old 11-13-2008
Thanks a lot otheus.

I modified the expression as \"([a-zA-Z0-9_$?\.=~<>{}#@`!$]*|[ \t\v\n])*\"

It works well for my requirement.

Thanks a lot
# 6  
Old 11-13-2008
Hrm, but it looks like you would miss spaces between words surrounded by quotes. Does it match:
Code:
"this is a long string"

# 7  
Old 11-14-2008
Hi,

Yes it matches strings such as "this is a long string" .

Dont know how it is able to parse even though i have not included a space character in the expression [a-zA-Z0-9_$?\.=~<>{}#@`!$]

I have even used another condition as :

[ \t]+ /* ignore whitespace */;

in my lex file .

Does this interfere with the expression used i.e \"([a-zA-Z0-9_$?\.=~<>{}#@`!$]*|[ \t\v\n])*\"


Thanks
Vishwa

Previous Thread | Next Thread
Test Your Knowledge in Computers #704
Difficulty: Medium
Under the same-origin policy, a web browser permits scripts contained in a first web page to access data in a second web page, but only if both web pages have the same origin.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to remove newline character if it is the only character in the entire file.?

I have a file which comes every day and the file data look's as below. Vi abc.txt a|b|c|d\n a|g|h|j\n Some times we receive the file with only a new line character in the file like vi abc.txt \n (8 Replies)
Discussion started by: rak Kundra
8 Replies

2. Shell Programming and Scripting

Remove last newline character..

Hi all.. I have a text file which looks like below: abcd efgh ijkl (blank space) I need to remove only the last (blank space) from the file. When I try wc -l the file name,the number of lines coming is 3 only, however blank space is there in the file. I have tried options like... (14 Replies)
Discussion started by: Sathya83aa
14 Replies

3. UNIX for Dummies Questions & Answers

Print a newline after first match in line

Hi everyone I have a file where CP occurs both within each line and at the very end: dwer 17 knsdask= * CP hwla 17 h'wopie un CP I would like to separate the line on the first CP to get: dwer 17 knsdask= * CP hwla 17 h'wopie un CP What I have so far is: awk '{for (x=1; x<NF; x++) ... (5 Replies)
Discussion started by: meet77
5 Replies

4. UNIX for Dummies Questions & Answers

Match EOF on newline in a file

Hi, i have a file where the end-of-file might be at the end of of a valid text line or on a new line case a) p q r s t u <eof> case b) p q r s t u <eof> case c) p q r s t u <no data, only carriage return> <eof> I have a requirement where <eof> line should not be read if it's... (3 Replies)
Discussion started by: ysrini
3 Replies

5. UNIX for Dummies Questions & Answers

newline character in a variable

variable="unix\nlinux" echo $variable expected output: unix linux :wall: can i do that ?? thanks in advance!! (3 Replies)
Discussion started by: sathish92
3 Replies

6. UNIX for Dummies Questions & Answers

Match newline character "\n" in lex

Hi everyone! This is my very first post, sorry if I'm not posting in the right category. I'm trying to match a newline "/n" using lex/yacc. For example, print(9,'\n',8) should print 9 8 now do I write a regular expression to match exactly " '\n' " Thanks! (1 Reply)
Discussion started by: code21
1 Replies

7. Programming

actions before parsing rules in lex n yacc

Hi , We have developed a grammer for our domain language using lex n yacc. I want to know is there any pre defined lex-yacc function which gets call before executing any rule (or rules). Oue requirement is, before processing any rule ,we want to perform some specific actions ? is there... (0 Replies)
Discussion started by: supritjain
0 Replies

8. UNIX for Dummies Questions & Answers

echo without newline character

hi, I have a for loop where in I write some file name to another file. I want to write all the filenames to another without any newlines. how can i avoid getting new lines with echo? Thanks, Srilaxmi (2 Replies)
Discussion started by: srilaxmi
2 Replies

9. UNIX for Dummies Questions & Answers

newline character

hi, I want to print the below lines "Message from bac logistics The Confirmation File has not been received." When i give like this in the code "Message from bac logistics\n The Confirmation File has not been received." It is giving only Message from bac logistics\n The... (9 Replies)
Discussion started by: trichyselva
9 Replies

10. Linux

lex for Chinese character

Hi, I need to read one chinese char using lex. I tried using "." ( period ) for pattern matching but in vain. Could anyone suggest me how do i proceeed. Sample pgm: to read a chinese char in single quotes. %{ #include <locale.h> %} %% \'.\' printf("SUCCESS\n"); . ... (0 Replies)
Discussion started by: suman_jakkula
0 Replies

Featured Tech Videos