Click here to Skip to main content
15,888,156 members
Home / Discussions / C#
   

C#

 
AnswerRe: Linq Pin
Christian Graus2-Aug-09 20:35
protectorChristian Graus2-Aug-09 20:35 
GeneralRe: Linq Pin
Sayed Sajid2-Aug-09 20:42
Sayed Sajid2-Aug-09 20:42 
GeneralRe: Linq Pin
riced2-Aug-09 23:18
riced2-Aug-09 23:18 
AnswerRe: Linq Pin
N a v a n e e t h2-Aug-09 20:42
N a v a n e e t h2-Aug-09 20:42 
QuestionFinding Nested ( ) pairs Pin
Adam R Harris2-Aug-09 20:07
Adam R Harris2-Aug-09 20:07 
AnswerRe: Finding Nested ( ) pairs Pin
OriginalGriff2-Aug-09 21:24
mveOriginalGriff2-Aug-09 21:24 
GeneralRe: Finding Nested ( ) pairs Pin
Adam R Harris3-Aug-09 9:05
Adam R Harris3-Aug-09 9:05 
AnswerRe: Finding Nested ( ) pairs Pin
Luc Pattyn3-Aug-09 0:27
sitebuilderLuc Pattyn3-Aug-09 0:27 
Hi,

you probably want to create a parser for your expressions, not just some code about parentheses.

So you first of all need a parser that understand identifiers, literals and operators, with operator precedence (identified by a value in a limited range, say add=0, sub=0, mul=1, div=1, etc up to 9). You need a little stack to hold operands, push operands as long as operator precedence is increasing, and popping operands and executing operators when precedence is decreasing or constant (and something special for right-to-left associativity as in a^b^c).

Now there are two ways to extend to expressions with parentheses:

1. the easy and slightly more expensive one uses recursion:
- create a method that implements the simple parser, and start scanning the input;
- once you hit a left parenthesis, you know a subexpression is going to start, hence let your method
call itself to parse that subexpression
- once you hit a right parenthesis or end-of-input, return.

2. the other way basically implements one overall parser:
- scan the expression left-to-right doing all the things your parser needs to do;
- once you hit a left parenthesis, adapt the current state and increase the precedence value of all future operators causing them to get executed sooner than everything outside the parentheses.


While (1) seems simpler than (2) when everything is fine, the recursive one becomes a little more complex if you want it to deal with mistakes properly: you have to unwind the recursions when invalid expressions are being fed, and you need to figure a way to generate proper error reporting, all of which is much simpler in a single overall parser.

Smile | :)

Luc Pattyn [Forum Guidelines] [My Articles]

The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.

GeneralRe: Finding Nested ( ) pairs Pin
Adam R Harris3-Aug-09 9:10
Adam R Harris3-Aug-09 9:10 
GeneralRe: Finding Nested ( ) pairs Pin
Luc Pattyn3-Aug-09 9:24
sitebuilderLuc Pattyn3-Aug-09 9:24 
AnswerRe: Finding Nested ( ) pairs Pin
PIEBALDconsult3-Aug-09 5:58
mvePIEBALDconsult3-Aug-09 5:58 
GeneralRe: Finding Nested ( ) pairs Pin
Adam R Harris3-Aug-09 9:12
Adam R Harris3-Aug-09 9:12 
QuestionExcel and C# Pin
veon cheng2-Aug-09 20:04
veon cheng2-Aug-09 20:04 
AnswerRe: Excel and C# Pin
Christian Graus2-Aug-09 20:14
protectorChristian Graus2-Aug-09 20:14 
GeneralRe: Excel and C# Pin
Adam R Harris2-Aug-09 20:16
Adam R Harris2-Aug-09 20:16 
AnswerRe: Excel and C# Pin
Adam R Harris2-Aug-09 20:15
Adam R Harris2-Aug-09 20:15 
GeneralRe: Excel and C# Pin
Mycroft Holmes2-Aug-09 21:21
professionalMycroft Holmes2-Aug-09 21:21 
GeneralRe: Excel and C# Pin
Adam R Harris3-Aug-09 9:14
Adam R Harris3-Aug-09 9:14 
GeneralRe: Excel and C# Pin
Mycroft Holmes3-Aug-09 12:21
professionalMycroft Holmes3-Aug-09 12:21 
Questionfind object in List<> specific values without loop Pin
pascal.schwartz2-Aug-09 20:00
pascal.schwartz2-Aug-09 20:00 
AnswerRe: find object in List<> specific values without loop Pin
Christian Graus2-Aug-09 20:11
protectorChristian Graus2-Aug-09 20:11 
AnswerRe: find object in List<> specific values without loop Pin
N a v a n e e t h2-Aug-09 20:50
N a v a n e e t h2-Aug-09 20:50 
GeneralRe: find object in List<> specific values without loop Pin
pascal.schwartz3-Aug-09 0:41
pascal.schwartz3-Aug-09 0:41 
GeneralRe: find object in List<> specific values without loop [modified] Pin
Alan N3-Aug-09 3:00
Alan N3-Aug-09 3:00 
GeneralRe: find object in List<> specific values without loop Pin
N a v a n e e t h3-Aug-09 16:20
N a v a n e e t h3-Aug-09 16:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.