0

我需要帮助才能找到结束标记行号。例如:

<tr>
  <td>...</td>
  <td>...</td>
</tr>

<tr>
  <td>...</td>
  <td>...</td>

<tr>
  <td>...</td>
  <td>...</td>
</tr>

在上述行中,第一个<tr>标签相应地打开和关闭。在第二个<tr>中,它打开但未关闭。没有关闭<tr>它打开第三个<tr>。在这种情况下,我需要将行号显示为错误。我可以找到行号打开标记。我需要找到结束标记行号。我在 WPF C# 中工作。

4

1 回答 1

2

You need to put them on "stack" while parsing. Push to your stack when having an open tag, remove in reverse order when a closing tag happen. If close-tag <> open tag type you have an error.

Count lines while parsing and when a mismatch occur print the line-number you're at. Count line numbers by counting line-feeds (char 10 for Windows/Linux, use 13 for Mac).

Compare closing tags with "/" + opening-tag

Stack (pesudo-representation to show the "mechanics"):

Parser res   Current stack
----------   -------------
Found TR  -> TR
Found TD  -> TR-TD
Found /TD -> TR (it matched last tag on stack) 
Found TD  -> TR-TD
Found /TD -> TR (it matched last tag on stack) 
Found /TR ->  (it matched last tag on stack - stack is empty, all ok) 

Found TR  -> TR
Found TD  -> TR-TD
Found /TD -> TR (it matched last tag on stack) 
Found TD  -> TR-TD
Found /TD -> TR (it matched last tag on stack) 
Found TR  -> TR (mismatch! /+TR <> TR as expected), print error + line number
于 2012-12-20T05:05:01.210 回答